C编程,清除字符串

时间:2014-02-23 23:04:56

标签: c string file

所以我有一些代码获取用户输入并在循环中进行比较,它将检查第一行,然后是第二行,然后是第三行,依此类推,但是程序没有得到另一个干净的字符串变量循环的下一个循环,如下所示:

char text[100], blank[100];
int c = 0, d = 0;

void space(void); 

int main()
{
    int loop=0;
    char str[512];
    char string[512];
    int line=1;
    int dis=1;
    int score=0;
    char text[64];

    FILE *fd;
    fd = fopen("Student Usernames.txt", "r"); //Should be test
    if (fd == NULL) 
    {
        printf("Failed to open file\n");
        exit(1);
    }
do
{
    printf("Enter the string: ");
    gets(text);
    while (text[c] != '\0')
   {
      if (!(text[c] == ' ' && text[c] == ' ')) {
       string[d] = text[c];
        d++;
      }
      c++;
   }
   blank[d] = '\0';
   printf("Text after removing blanks\n%s\n", string);

   getch();

    for(loop = 0;loop<line;++loop)
    {
        fgets(str, sizeof(str), fd);
    }
    printf("\nLine %d: %s\n", dis, str);
    dis=dis+1;
     str[strlen(str)-1] = '\0';
    if(strcmp(string,str) == 0 )
     {
     printf("Match\n");
     score=score+2;
      }
         else
         {
         printf("Nope\n");
         score=score+1;
         }
     getch();
}
while(!feof(fd));
printf("Score: %d",score);
getch();
}

例如:输入:Apple(第一个字),第一行= Apple,匹配,输入:香蕉,第二行= Applea

显然,该程序已添加到第一个输入,我想知道如何更改此

编辑:在每次循环后将c和d重置为0时,输出现在为“AppleBanana”

0 个答案:

没有答案