int main()
{
int input;
printf("input lenth : \n");
scanf("%d", &input);
while(getchar()!='\n')
continue;
printf("input str : \n");
char* sentence = (char*)malloc(sizeof(char) * input);
fgets(sentence, sizeof(sentence), stdin);
reverse(sentence, strlen(sentence));
free(sentence);
return 0;
}
I learn fgets
can get space.
so I malloc
enough space to sentence
ex) 100
and I input I am a boy
But when I print my sentence
, IT just print I am a
...
what's th problem?
This statement
fgets(sentence, sizeof(sentence), stdin);
is incorrect. It seems you mean
fgets(sentence, input, stdin);
Otherwise sizeof( sentence )
will yield the size of a pointer declared like
char* sentence = (char*)malloc(sizeof(char) * input);
Pay attention to that the function fgets
can append the new line character '\n'
to the entered string. You should remove it like
sentence[ strcspn( sentence, "\n" ) ] = '\0';