Well as Narue suggested..
scanf function leaves '\n' into buffer, so when it reaches
while((character=getchar())!= '\n')
reads '\n' from buffer and loop terminates.
after this you are using
puts(sentence);
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'.
Try this:
//Character Manipulation
#include <stdio.h>
void jsw_flush(FILE *in) // Function copied from older post of Narue
{
int ch;
do
ch = getc(in);
while (ch != '\n' && ch != EOF);
clearerr(in);
}
int main()
{
int x;
//for loops
int counter=0;
//for input
char character;
char sentence[20];
printf("Press 1 to use getchar \n Press 2 to use gets \n Press 3 to use sscan \n ");
scanf("%d",&x);
jsw_flush(stdin); // flush input buffer
switch(x)
{
case 1:
printf("Enter something\n");
while((character=getchar())!= '\n')
{
sentence[counter++]=character;
}
sentence[counter] = '\0';
puts(sentence);
break;
}
}