I'm creating a program that reverses every line of input.
This is my code:
#include <stdio.h>
#include <string.h>
int main()
{
char c[100];
while((fgets(c,100,stdin)) != NULL)
{
c[strlen(c) - 1] = '\0';
for(int i=strlen(c); i>=0; i--)
{
printf("%c",c[i]);
}
}
}
My test input:
abc 123
lorem ipsum
dolor sit amet
I can print the first line reversed just fine:
abc 123
321 cba
But when I start to enter the next line of input, it's next to the reversed input, so the full program run for the test input looks like this:
abc 123
321 cbalorem ipsum
muspi meroldolor sit amet
tema tis rolod
When it should look like this:
abc 123
321 cba
lorem ipsum
muspi merol
dolor sit amet
tema tis rolod
As a workaround, I press Enter again after the output to be able to enter the next line of input on its own line, but I don't understand why I need to do that.