Below is the code to print each word backward
Example if Input is “This is Sparta”
Out put should be “sihT si atrapS”
Example if Input is “This is Sparta”
Out put should be “sihT si atrapS”
Thanks to Dhaval for suggesting this approach
#include<stdio.h> #include<string.h> int main() { int j,i; int lastWhiteSpace = 0; //there were no white spaces char str[100]="This is Sparta"; printf("nString in Reverse Ordern"); i=0; while(str[i]!='') { if(str[i]==' ') { for(j=i-1;j>=lastWhiteSpace;j--) printf("%c",str[j]); //Loop starts from last char and decrements upto the character after the last white space printf(" "); lastWhiteSpace = i + 1; //the character after this white space } i++; } //later edit for(j=i-1;j>=lastWhiteSpace;j--) printf("%c",str[j]); printf("n"); }