Read a string from user using gets () function. Now travel the string until a space (ASCII Code 32) found and copy the characters in a variable called 'word'. If a space found, then it considers a word and terminate it with a null character. Now reverse the word and store it into an output string. This procedure remains continue until the string is ended.
#include <stdio.h> #include <string.h> #include <conio.h> int main() { char str[30],rev[30],word[30],i,w=0; printf("Enter A String: "); gets(str);//READ A STRING strcat(str," ");//ADD A SPACE AT END OF STR strcpy(word,"");//BLANK THE TEMPORARY WORD strcpy(rev,"");//BLANK THE OUTPUT STRING [REV] for(i=0;str[i]!='\0';i++) { if(str[i]!=' ') { //CHARACTER ATTACH TO WORD word[w++]=str[i]; } else { word[w]='\0'; strrev(word);//reverse the word //attach the reversed word into output string [rev] if(strlen(rev)==0) { strcpy(rev,word); } else { strcat(rev," "); strcat(rev,word); } //RESET THE VARIABLE WORD AND W strcpy(word,""); w=0; } } //PRINT THE OUTPUT printf("Result : "); puts(rev); return 0; }