-2
#include <stdio.h>
int main(){
    char a[20],b[20];
    int i,c=0,m=0;
    
    fgets(a,20,stdin);
    fgets(b,20,stdin);
    while(a[c]!=0){
        c++;
    }
    for(i=0;i<c;i++)
    { 
        if(a[i]==b[i])
        m=m+1;
    }
    printf("%d",m);      
}

when i use fgets to input it doesn't work but if I use gets it works. here i am comparing elements of string and showing number of similar element. for eg input a: 1010101 b: 9898101 then it give 4 as output, but when I use gets it gives 3.

Vlad from Moscow
  • 301,070
  • 26
  • 186
  • 335
  • 2
    Please read more abpout [`fgets`](https://en.cppreference.com/w/c/io/fgets) and what it stores in the buffer. – Some programmer dude Jun 21 '21 at 12:11
  • 4
    Also please tell us what "it doesn't work" really means. ***How*** Doesn't it work? For some specified input, what is the expected and actual output? And have you tried to step through the code statement by statement (while monitoring variables and their values) to see what really happens? And what's wrong with using the standard `strlen` function to get a strings length? – Some programmer dude Jun 21 '21 at 12:14
  • 2
    Please give more information about how and when it "doesn't work" - are you attempting to enter more than 20 characters, so `gets` corrupts your memory (potentially) but `fgets` limits your input? – Adrian Mole Jun 21 '21 at 12:14
  • can you tell me some good sites where I can study about programing – Gautam Bisht Jun 21 '21 at 12:14
  • 1
    What exactly "doesn't work"? The code with fgets seems to work fine: `printf 'foo\nfoo\n' | ./a.out` prints `4` as expected. What is your input? What output do you get? How does your actual output differ from your expectation? – William Pursell Jun 21 '21 at 12:15
  • Edit your post to include an exact sample input you have used (for both `fget()` statements) and the exact value you got for those entries. _"does not work"_ is difficult to interpret without knowing a little more. – ryyker Jun 21 '21 at 12:19
  • Perhaps you need to invest in [some good books](https://stackoverflow.com/questions/388242/the-definitive-c-book-guide-and-list) and take some classes? Because that's really the best way to learn programming in general. – Some programmer dude Jun 21 '21 at 12:19
  • 1
    `fgets()` reads and includes a newline in the input buffer. `gets()` reads but does not include newline in the input buffer. If your input is 18 or less chars long (not counting the newline), both should work as posted. If your input is exactly 19 chars long (not counting the newline) the `fgets()` version reads the newline on the 2nd `fgets()`. If your input is 20 chars or more the `gets()` version invokes Undefined Behaviour. – pmg Jun 21 '21 at 12:20
  • when i use fgets it gives increased value of number of similar elements by a margin of 1, – Gautam Bisht Jun 21 '21 at 12:22
  • That explains your confusion. When you enter a line with N characters, the string stored by `fgets` has length N+1. This is expected behavior. – William Pursell Jun 21 '21 at 12:23
  • The previous comment explains why that will always be the case. One reads in the `\n` (newline character) the other does not. – ryyker Jun 21 '21 at 12:24
  • `fgets()` reads **and includes** the newline. `fgets(buf, 20, stdin);` when presented with `"foo\n"` will make `buf` contents `"foo\n"`. `gets()` reads **and does not include** the newline (contents `"foo"`). – pmg Jun 21 '21 at 12:24
  • thanks a lot guys, for clearing doubt – Gautam Bisht Jun 21 '21 at 12:26

1 Answers1

1

Opposite to the unsafe function gets (that is not supported by the C Standard) the function fgets can append the inputted string with the new line character '\n' if the array where characters are read has enough space. So if two strings obtained by calls of fgets have equal lengths and the new line character is stored in them then it will be count in your second for loop.

To remove the new line character '\n' from a string obtained by a call of fgets you can use for example the following approach

#include <string.h>

//...

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

In general your program is incorrect because the user can enter strings of different lengths.

Vlad from Moscow
  • 301,070
  • 26
  • 186
  • 335