I'm trying to reverse a string word by word in C. So far, I succeeded in reversing words, but there is a new line issue I don't understand why happening. The code is below.
#include <stdio.h>
void reverse_string(char input[], int start, int end)
{
char temp;
while (start < end)
{
temp = input[start];
input[start] = input[end];
input[end] = temp;
start++;
end--;
}
}
int main()
{
char input[100];
printf("Input > ");
fgets(input, 100, stdin);
int start = 0, end = 0;
while (input[end])
{
for (end = start; input[end] && input[end] != ' '; end++);
reverse_string(input, start, end - 1);
start = end + 1;
}
printf("%s", input);
return 0;
}
If an input is "Today is a sunny day.", The result would be
yadoT si a ynnu
.yad
What I want is
yadoT si a ynnu .yad
How should I edit the code above so that every reversed word will be on the same line?