C 如何在每个单词之前打印字符串中每个单词的长度
我想打印字符串中每个单词的长度 我试过了,但没有得到正确的答案。运行代码后,它将在单词后打印每个单词的长度,而不是在每个单词前打印C 如何在每个单词之前打印字符串中每个单词的长度,c,string,C,String,我想打印字符串中每个单词的长度 我试过了,但没有得到正确的答案。运行代码后,它将在单词后打印每个单词的长度,而不是在每个单词前打印 char str[20] = "I Love India"; int i, n, count = 0; n = strlen(str); for (i = 0; i <= n; i++) { if (str[i] == ' ' || str[i] == '\0') { printf("%d", count); cou
char str[20] = "I Love India";
int i, n, count = 0;
n = strlen(str);
for (i = 0; i <= n; i++) {
if (str[i] == ' ' || str[i] == '\0') {
printf("%d", count);
count = 0;
} else {
printf("%c", str[i]);
count++;
}
}
charstr[20]=“我爱印度”;
int i,n,count=0;
n=strlen(str);
对于(i=0;i这是您想要的:
#include <stdio.h>
#include <string.h>
int main()
{
char str[20]="I Love India";
char buf[20];
int i,n,count=0;
n=strlen(str);
for (i=0; i <= n; i++) {
if(str[i]==' ' || str[i]=='\0'){
buf[count] = '\0';
printf("%d", count); /* Print the size of the last word */
printf("%s", buf); /* Print the buffer */
memset(buf, 0, sizeof(buf)); /* Clear the buffer */
count = 0;
} else {
buf[count] = str[i];
count++;
}
}
return 0;
}
#包括
#包括
int main()
{
char str[20]=“我爱印度”;
char-buf[20];
int i,n,count=0;
n=strlen(str);
对于(i=0;i这是您想要的:
#include <stdio.h>
#include <string.h>
int main()
{
char str[20]="I Love India";
char buf[20];
int i,n,count=0;
n=strlen(str);
for (i=0; i <= n; i++) {
if(str[i]==' ' || str[i]=='\0'){
buf[count] = '\0';
printf("%d", count); /* Print the size of the last word */
printf("%s", buf); /* Print the buffer */
memset(buf, 0, sizeof(buf)); /* Clear the buffer */
count = 0;
} else {
buf[count] = str[i];
count++;
}
}
return 0;
}
#包括
#包括
int main()
{
char str[20]=“我爱印度”;
char-buf[20];
int i,n,count=0;
n=strlen(str);
对于(i=0;i你的方法是错误的,因为你在长度之前打印单词。所以你需要先计算长度,然后打印它,然后打印单词
可能是这样的:
int main(void)
{
char str[20]="I Love India";
size_t i = 0;
while(str[i])
{
if (str[i] == ' ') // consider using the isspace function instead
{
// Print the space
printf(" ");
++i;
}
else
{
size_t j = i;
size_t count = 0;
// Calculate word len
while(str[j] && str[j] != ' ')
{
++count;
++j;
}
// Print word len
printf("%zu", count);
// Print word
while(i<j)
{
printf("%c", str[i]);
++i;
}
}
}
}
int main(无效)
{
char str[20]=“我爱印度”;
尺寸i=0;
while(str[i])
{
如果(STR [i]=='')/ /考虑使用iStof函数
{
//打印空间
printf(“”);
++一,;
}
其他的
{
尺寸j=i;
大小\u t计数=0;
//计算字长
while(str[j]&&str[j]!=“”)
{
++计数;
++j;
}
//印刷字透镜
printf(“%zu”,计数);
//印刷字
而(i你的方法是错误的,因为你先打印单词的长度,然后再打印单词。所以你需要先计算长度,然后再打印单词
可能是这样的:
int main(void)
{
char str[20]="I Love India";
size_t i = 0;
while(str[i])
{
if (str[i] == ' ') // consider using the isspace function instead
{
// Print the space
printf(" ");
++i;
}
else
{
size_t j = i;
size_t count = 0;
// Calculate word len
while(str[j] && str[j] != ' ')
{
++count;
++j;
}
// Print word len
printf("%zu", count);
// Print word
while(i<j)
{
printf("%c", str[i]);
++i;
}
}
}
}
int main(无效)
{
char str[20]=“我爱印度”;
尺寸i=0;
while(str[i])
{
如果(STR [i]=='')/ /考虑使用iStof函数
{
//打印空间
printf(“”);
++一,;
}
其他的
{
尺寸j=i;
大小\u t计数=0;
//计算字长
while(str[j]&&str[j]!=“”)
{
++计数;
++j;
}
//印刷字透镜
printf(“%zu”,计数);
//印刷字
while(i您可以按建议使用strtok。在strtok修改传递的字符串时,您可能需要复制原始字符串。此外,strtok不是线程安全的,在使用多线程程序时必须用strtok_r替换
#include <stdio.h>
#include <stdlib.h>
/* for strtok */
#include <string.h>
int main() {
char str[20] = "I Love India";
int n;
char* tok = strtok(str, " ");
while (tok != NULL) {
n = strlen(tok);
printf("%d%s ", n, tok);
tok = strtok(NULL, " ");
}
return EXIT_SUCCESS;
}
#包括
#包括
/*斯特托克*/
#包括
int main(){
char str[20]=“我爱印度”;
int n;
char*tok=strtok(str,“”);
while(tok!=NULL){
n=strlen(tok);
printf(“%d%s”,n,tok);
tok=strtok(空,“”);
}
返回退出成功;
}
您可以按建议使用strtok。在strtok修改传递的字符串时,您可能需要复制原始字符串。此外,strtok不是线程安全的,在使用多线程程序时必须用strtok\r替换
#include <stdio.h>
#include <stdlib.h>
/* for strtok */
#include <string.h>
int main() {
char str[20] = "I Love India";
int n;
char* tok = strtok(str, " ");
while (tok != NULL) {
n = strlen(tok);
printf("%d%s ", n, tok);
tok = strtok(NULL, " ");
}
return EXIT_SUCCESS;
}
#包括
#包括
/*斯特托克*/
#包括
int main(){
char str[20]=“我爱印度”;
int n;
char*tok=strtok(str,“”);
while(tok!=NULL){
n=strlen(tok);
printf(“%d%s”,n,tok);
tok=strtok(空,“”);
}
返回退出成功;
}
在打印单词之前,您需要计算并打印每个单词的长度
下面是一个使用strcspn()
的简单解决方案,这是一个应该经常使用的标准函数:
#include <stdio.h>
#include <string.h>
int main() {
char str[20] = "I Love India";
char *p;
int n;
for (p = str; *p;) {
if (*p == ' ') {
putchar(*p++);
} else {
n = strcspn(p, " "); // compute the length of the word
printf("%d%.*s", n, n, p);
p += n;
}
}
printf("\n");
return 0;
}
#包括
#包括
int main(){
char str[20]=“我爱印度”;
char*p;
int n;
对于(p=str;*p;){
如果(*p==''){
putchar(*p++);
}否则{
n=strcspn(p,“”;//计算单词的长度
printf(“%d%.*s”,n,n,p);
p+=n;
}
}
printf(“\n”);
返回0;
}
在打印单词之前,您需要计算并打印每个单词的长度
下面是一个使用strcspn()
的简单解决方案,这是一个应该经常使用的标准函数:
#include <stdio.h>
#include <string.h>
int main() {
char str[20] = "I Love India";
char *p;
int n;
for (p = str; *p;) {
if (*p == ' ') {
putchar(*p++);
} else {
n = strcspn(p, " "); // compute the length of the word
printf("%d%.*s", n, n, p);
p += n;
}
}
printf("\n");
return 0;
}
#包括
#包括
int main(){
char str[20]=“我爱印度”;
char*p;
int n;
对于(p=str;*p;){
如果(*p==''){
putchar(*p++);
}否则{
n=strcspn(p,“”;//计算单词的长度
printf(“%d%.*s”,n,n,p);
p+=n;
}
}
printf(“\n”);
返回0;
}
然后您需要在单词之前打印计数。第一:记住您在字符串中的位置。第二:计算字数。打印计数。打印记住的单词(您可以使用printf(“%s”,长度,指针);
)。然后增加你在字符串中的位置。按照你现在的做法,计数只能在单词之后进行。回到你的实验室,思考如何在单词之前打印。需要不同的方法。你可能对学习函数感兴趣。@HareramShah:你可以通过单击灰色复选标记来接受其中一个答案低于其分数。然后需要在单词之前打印计数。第一:记住字符串中的位置。第二:计算字数。打印计数。打印记住的单词(可以使用printf(“%s”,长度,指针);
)。然后增加你在字符串中的位置。按照你现在的做法,计数只能在单词之后进行。回到你的实验室,思考如何在单词之前打印。需要不同的方法。你可能对学习函数感兴趣。@HareramShah:你可以通过单击灰色复选标记来接受其中一个答案低于其分数。感谢您提供解决方案。但它的输出为“1I@4Love5India“。中不需要“@”output@HareramShah它现在可以工作了,在打印之前,我必须在buf的末尾添加一个空终止符。感谢您提供解决方案。但是