如何在每个单词之前打印字符串中每个单词的长度

时间:2019-03-31 08:38:35

标签: 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);
        count = 0;
    } else {
        printf("%c", str[i]);
        count++;
    }
}

除了输出为1I 4Love 5India以外,我的实际输出为I1 Love4 India5

4 个答案:

答案 0 :(得分:1)

您可以将Some programmer dude用作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;
}

答案 1 :(得分:1)

您要在打印单词之前计算并打印每个单词的长度。

这是使用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; } 是应该经常使用的标准功能:

WITH 
MEMBER [Measures].[Internet Sales Amount Rank] AS
RANK( ([Product].[Category].currentmember,[Product].[Subcategory].CurrentMember),
ORDER( ([Product].[Category].currentmember,[Product].[Subcategory].[Subcategory].Members) , [Measures].[Internet Sales Amount], BDESC)
) 

select 
non empty
([Product].[Category].[Category],filter([Product].[Subcategory].[Subcategory],[Measures].[Internet Sales Amount Rank]<4))
on columns,
non empty 
([Customer].[Yearly Income].[Yearly Income],[Measures].[Internet Sales Amount])
on rows 
from [Adventure Works]

答案 2 :(得分:0)

这就是您想要的:

#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;
}

您将希望保留当前正在计算的单词的缓冲区。 ( buf ) 每次 count 都增加一个空格或0 /。然后,当它是空格或0 /时,先打印 count ,然后打印 buf 。然后,我们将清除 buf 并将 count 设置为0,以便变量 i 仍在整个字符串 str < / em>,但是我们将从0开始将单词插入 buf

答案 3 :(得分:0)

您的方法是错误的,因为您在长度之前打印单词。因此,您需要先计算长度,然后打印然后再打印单词。

可能是这样的:

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;
            }
        }
    }
}

基本思想是为字符串提供两个索引变量ij。索引i在单词的第一个字符处,索引j用于查找单词的结尾。一旦找到单词的结尾,就可以打印长度和单词。