Basically this program should take input from the user and reverse each word in the sentence. So if I ever "Hello World" is should print "olleH dlroW", but as of now it prints "dlroW olleH". So my program reverses the whole sentence and not just the specific words. Not really sure how to approach it. I gave it my best.
#include <stdio.h>
#include <string.h>
void reverse(char *str, int start, int end)
{
char temp;
while (start < end)
{
if(isspace(str[start]))
{
printf("%s ", str);
start++;
}
else
{
temp = str[start];
str[start] = str[end];
str[end] = temp;
start++;
end--;
}
}
return;
}
int main()
{
char word[256];
int i=0, bound;
printf("Enter a string\n");
fgets(word, 256, stdin);
printf("%s", word);
bound = strlen(word);
reverse(word, i, bound-1);
printf("%s\n", word);
}
fix like this
Track the point where you start and when you find a space - both integers. If you find a space get a substring from 'start'(first letter) to 'end'(space). Reverse the substring and put it back to original string and go on.
Just change your reverse method to this. I hope this helps.
results from test