先把整个字符串逆序,再把每个单词逆序
#include<stdio.h>
#include<string.h>
void reverse(char *left, char *right)
{
while(left < right)
{
char tmp = *left;
*left = *right;
*right = tmp;
left++;
right--;
}
}
int main()
{
char arr[1000];
gets(arr);
int len = strlen(arr);
reverse(arr, arr+len-1);
int j = 0;
for(int i=0; i<=len; i++)
{
if(arr[i] == ' ' || arr[i] == '\0')
{
reverse(arr+j, arr+i-1);
j = i+1;
}
}
printf("%s", arr);
}