The solutions tell how to deal with this. One thing I noticed were these lines :
char a[20];
fgets(a,50,stdin);
You are telling
fgets
to accept more data than the array
a
can hold and that can result in unexpected behavior from your program.
A better way to deal with this is to define a value for the size of the buffer and use that with
fgets
. Here is how you can do that.
#define BUFFER_SIZE 49
char a[ BUFFER_SIZE + 1 ]; fgets( a, BUFFER_SIZE, stdin );
Now your code will not accept more data than the array can contain.