I have this string s1 = \"My name is X Y Z\"
and I want to reverse the order of the words so that s1 = \"Z Y X is name My\"
.
I can do it u
This is assuming all words are separated by spaces:
#include
#include
int main()
{
char string[] = "What are you looking at";
int i, n = strlen(string);
int tail = n-1;
for(i=n-1;i>=0;i--)
{
if(string[i] == ' ' || i == 0)
{
int cursor = (i==0? i: i+1);
while(cursor <= tail)
printf("%c", string[cursor++]);
printf(" ");
tail = i-1;
}
}
return 0;
}