Hi
I'm writing a program to reverse words. When I input, say, "they call me jim" I get this:
fjim
fmefcallthey
instead of this:
jim me call they
So I'm not exactly sure where the 'f' is coming from, and where the newline is coming from. I guess I'm kinda confused here. I can't really think of a good reason why the 'f's' should be there. Thanks for any help you can give me.
Code:#include <stdio.h>
#include <string.h>
int main()
{
char buffer[30];
int i, count;
printf("enter a string: \n");
fgets(buffer, 29, stdin);
i = strlen(buffer) - 1;
while (i > -1)
{
if ((buffer[i] == ' ') && (buffer[i] != '\0'))
{
putc(" ", stdout);
count = i - 1;
i++;
while ((buffer[i] != ' ') && (buffer[i] != '\0'))
{
putc(buffer[i], stdout);
i++;
}
i = count;
}
else
i--;
}
for (i = 0; buffer[i] != ' '; i++)
{
putc(buffer[i], stdout);
}
return 0;
}