1

I want to use a loop here. But my second fgets dont work properly!
In 1st loop it was okay! but after that it skips its second fgets!
How to fix it?
Thanks

#include<stdio.h>
#include<string.h>
int main (void)
{
char first_line[1000];
char second_line[2];
int i,n,j,k;
int count=0,flag=0;
scanf("%d ",&k);
for(int m=0; m<k; m++)
{
    fgets(first_line, 1000, stdin);
    fgets(second_line, 2, stdin);
    for(i=0; i<strlen(first_line); i++)
    {
        if(second_line[0]==first_line[i])
        {
            flag=1;
            count++;
        }
    }
    first_line[strlen(first_line)-1] = '\0';
    if(flag==1)
        printf("Occurrence of '%c' in %s = %d",second_line[0],first_line,count);

    else
        printf("%c isn't present",second_line[0]);
    count=0;
    flag=0;
}

return 0;
}
Tonmoy Mohajan
  • 91
  • 2
  • 4
  • 13

3 Answers3

6

The problem is that the array second_line is declared as having only two elements.

char second_line[2];

Thus after this call

fgets(second_line, 2, stdin);

it can not accommodate the new line character that will be still in the input buffer. And the first call of fgets reads an empty string due to the presence of the new line character in the input buffer.

At least you should Write

char second_line[3];

//...

fgets(second_line, sizeof( second_line ), stdin);

Take into account that in general this approach to remove the new line character from a string is wrong

first_line[strlen(first_line)-1] = '\0';

because it is not necessary that the string indeed contains a new line character.

Instead write

first_line[ strcspn( first_line, "\n" ) ] = '\0';
Vlad from Moscow
  • 301,070
  • 26
  • 186
  • 335
0

The second line is actually a character in this case. There is fgetc() in C which takes in a character. That can be used instead of fgets() in this case. Besides the issue with fgets() for second line, there is an issue with the scanf("%d ", ) statement. A space after format specifier causes behavior which you didn't expect. Also the newline character that has entered after entering a numerical value also causes skipping prompt behavior. This may not the best solution, but the following code resolves this skipping prompt behavior. There is fgets(caGarbage, sizeof caGarbage, stdin) statement after scanf() and fgetc(). This statement consumes newline character and thus resolves this skipping prompt behavior.

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

 int 
 main(void)
 {
     char first_line[1000];
     char cC;
     char caGarbage[50];
     int i,n,j,k;
     int count=0,flag=0;

     printf("Enter count:");
     scanf("%d",&k);
     fgets( caGarbage, sizeof caGarbage, stdin);//Consumes newline character.

     for(int m=0; m<k; m++)
     {
         printf("Insert first line:");
         fgets(first_line, 1000, stdin);

         printf("Insert a character:");
         cC = fgetc( stdin );

         fgets( caGarbage, sizeof caGarbage, stdin); //Consumes newline character

         for(i=0; i<strlen(first_line); i++)
         {
            if(cC == first_line[i])
            {
                flag=1;
                count++;
            }
         }

         first_line[strcspn( first_line, "\n" )] = '\0';

         if(flag==1)
            printf("Occurrence of '%c' in %s = %d",cC,first_line,count);
         else
            printf("%c isn't present",cC);

         printf("\n");

         count=0;
         flag=0;
     }

     return 0;
 }
Nguai al
  • 958
  • 5
  • 15
  • Can you say more details please? How much space/new line taken by scanf("%d",&k) and cC = fgetc( stdin ) ? – Tonmoy Mohajan May 05 '17 at 07:29
  • Since k is an integer, it will take take numerical value, within the boundary of max and min integer. fgetc() takes just one character. Both cases, newline char is sent to input buffer. The newline character from the input butter gets carried over to next input statement. – Nguai al May 05 '17 at 07:44
  • 1
    why here not taken fgets( caGarbage, sizeof caGarbage, stdin); after fgets(first_line, 1000, stdin); to consumes newline character? @Nguai – Tonmoy Mohajan May 05 '17 at 08:08
  • @TonmoyMohajan - A great question!!! Do`man fgets` on linux. It says: fgets() reads in at most one less than size characters from stream and stores them into the buffer pointed to by s. Reading stops after an EOF or a newline ` If a newline is read, it is stored into the buffer.` A terminating null byte ('\0') is stored after the last character in the buffer. So newline char gets stored into `first_line` in this case. This is what is good about fgets(). It takes care of newline character, unlike scanf() nor fetc(). – Nguai al May 05 '17 at 15:37
0

You don't understand the details about scanf() and fgets() when you input a series char from the keyboard, these chars were saved in the inputbuf not to your process. when you enter [Enter] , the chars in the inputbuf were send to your process(the first is scanf(), second is fgets()), scanf() can chose the numbers, the space key form the inputbuf. but it skip the Enter and stoped, so the '\n' was still in the inputbuf but fgets() does not. fgets() gets the any char in the inputbuf, include ' ' and '\n'(it stoped while geting a '\n').
the difference between scanf() and fgets() was a annoyance when you use them at the same time.