Iam not sure about this solution..
every string is ended with null character or \n.try this block...
'\0'indicated end of the String So when you will use this null character then after single string only your loop will terminate and when you will use '\n' it should terminate after the end of the line.
Well as Narue suggested..
scanf function leaves '\n' into buffer, so when it reaches
reads '\n' from buffer and loop terminates.
after this you are using
it will print some garbage value on screen bcoz your string is not terminated by a '\0' char.
to overcome these problems after using scanf function you need to flush input stream and after also terminate your string with '\0'.
void jsw_flush(FILE *in) // Function copied from older post of Narue
ch = getc(in);
while (ch != '\n' && ch != EOF);
printf("Press 1 to use getchar \n Press 2 to use gets \n Press 3 to use sscan \n ");
jsw_flush(stdin); // flush input buffer
sentence[counter] = '\0';
Firstly, I suggest always checking the return value of scanf. If you don't check the return value, and someone enters "gibberish" instead of "42", then you end up with garbage in your variable. You'd want to tell the user they entered incorrectly, wouldn't you?
Same thing with your getchar return value. If the user closes stdin (by pressing CTRL+Z in Windows or ^d in *nix), how do you think that will impact upon getchar? You should store the result of getchar into an int so you can tell the difference between EOF and an actual character.