this is my source code for printing each word in string backwards. but this code is just printing the 1st word backward and not the entire string. after printing 1st word backward it generates a pattern of 1st and 2nd words printed backwards. If while is used instead of if then it generates an infinite loop.
// Print an Entered String Backwards
#include <stdio.h>
#include <string.h>
int main()
{
int j,i;
char str[100];
printf("Enter String\n");
gets(str);
printf("\nString in Reverse Order\n");
i=0;
while(str[i]!='\0')
{
if(str[i]==' ')
{
for(j=i-1;j>=0;j--) //Loop starts from last char and decrements upto 0th char
printf("%c",str[j]);
printf(" ");
}
i++;
}
}