#include<iostream>
using namespace std;
void swap (char *chDest, char *chSrc)
{
char temp;
temp = *chDest;
*chDest = *chSrc;
*chSrc = temp;
}
void reverseStr(char *Str)
{
char *head = Str;
char *tail = Str;
char *ptr = Str;
//翻转每个单词
while (*ptr != '\0')
{
ptr++;
if (*ptr == ' ' || *ptr == '\0')
{
tail = ptr-1;
while (head < tail)
swap(head++,tail--);
head = tail = ptr+1;
}
}
//翻转整个字符串
head = Str;
tail = ptr-1;
while (head < tail)
swap(head++,tail--);
}
int main()
{
char string[100];
cin.getline(string,100);
reverseStr(string);
cout << string;
cin.get();
cin.get();
return 0;
}
运行结果: