字符串长度错误

时间:2015-04-01 13:00:25

标签: c string loops

我正在使用fgets函数从stdin读取字符串,然后尝试打印字符串的长度,但我总是第一次将字符串的长度设为1 这是我的代码

#incldue<stdio.h>
#include<string.h>
int main(void)
{
   printf("\n Enter the no of test cases");
   scanf("%d",&t);
   int i,j;
   for(i=0;i<t;++i)
   {
     char song[500],val[28];
     int k=0,x=0;
     fgets(song,500,stdin);
     int len=strlen(song);
     printf("\nlen=%d",len);
   }
  return 0;
}

我总是得到1作为第一个测试用例的长度:/ 请告诉我出错的地方

3 个答案:

答案 0 :(得分:5)

您没有清除输入缓冲区。将输入值提供给第一个scanf后,新行就会出现。因此fgets将无法获得用户的输入。 换行符将放在第一个(song[0])位置的缓冲区中。所以这就是strlen返回值1的原因。

fgets

之前加上这一行
int c;
if ( i == 0 )
    while((c=getchar()) != '\n' && c != EOF );
fgets(song,500,stdin);

或者在获得scanf的输入后放置此行。

scanf("%d",&t);
while((c=getchar()) != '\n' && c != EOF );

答案 1 :(得分:3)

\n输入字符串中包含scanf(以及在 { }开头的C声明变量中)。

另请注意,len将包含\n字符。

#include<stdio.h>
#include<string.h>
int main(void)
{
    int t, i;
    printf("Enter the no of test cases: ");
    scanf("%d\n",&t);
    for(i=0;i<t;++i) {
        char song[500];
        int len;

        fgets(song,500,stdin);
        len=strlen(song);
        printf("len=%d\n",len);
    }
    return 0;
}

更新

如果你需要处理奇怪的输入,只需使用fgets(从 len 中移除\n)。

#include<stdio.h>
#include<string.h>
#include<stdlib.h>
int main(void)
{
    char song[500];
    int t, i, len;  
    printf("Enter the no of test cases: ");
    fgets(song,500,stdin);
    t = atoi(song);
    for(i=0;i<t;++i) {
        fgets(song,500,stdin);
        if ((len=strlen(song)) > 0) {
            song[--len] = '\0';
            printf("len=%d\n",len);
        }
    }
    return 0;
}

答案 2 :(得分:0)

使用scanf(或其亲属)时,检查函数的返回非常重要。 scanf返回正确匹配和分配的输入值的数量。如果字符不正确或字符不足,scanf会遇到匹配输入失败。快速if语句就足够了:

    if (!scanf ("%d", &t)) {
        fprintf (stderr, "error: invalid type or number for test cases.\n");
        return 1;
    }

如前所述,fgets会在song中读取并包含尾随的newline字符。通常,您需要删除尾随newline以防止在代码中通过各种字符串散布分散的换行符。 (更不用说为length=5查看data有点奇怪了)在致电newline后删除fgets的一种简单方法是:

            len = strlen (song);
            while (len && song[len-1] == '\n')  /* strip newline */
                song[--len] = 0;

scanf返回的测试放在一起,清空输入缓冲区,并在newline之后删除fgets,您的代码看起来类似于:

#include <stdio.h>
#include <string.h>

int main (void)
{
    int c = 0;
    int i = 0;
    int t = 0;

    printf ("\n Enter the no of test cases: ");
    if (!scanf ("%d", &t)) {
        fprintf (stderr, "error: invalid type or number for test cases.\n");
        return 1;
    }
    while ((c = getchar()) != '\n' && c != EOF);

    for (i = 0; i < t; ++i) {

        char song[500] = { 0 };
        size_t len = 0;

        if (printf ("\n    case [%d] : ", i) && fgets (song, 500, stdin))
        {
            len = strlen (song);
            while (len && song[len-1] == '\n')  /* strip newline */
                song[--len] = 0;
        }
        printf ("         len : %zu\n", len);
    }

    printf ("\n");
    return 0;
}

<强>输出

$ ./bin/scanf_rd_int

 Enter the no of test cases: 2

    case [0] : this is case one - 28 chars.
         len : 28

    case [1] : this is case two -- 29 chars.
         len : 29
相关问题