比如将“Alice call Jack”转换为“Jack call Alice”
#include <iostream>
#include <Windows.h>
#include <string>
using namespace std;
int main()
{
int i = 0, j = 0, k1, k2;
string str;
char tmp;
cout << "请输入一句话:";
getline(cin, str);
while (str[i])
{
while (str[i] == ' ') i++;
j = i;
while (str[j] && str[j] != ' ')j++;
for (k1 = i, k2 = j - 1; k1 < k2;k1++, k2--)
{
tmp = str[k1];
str[k1] = str[k2];
str[k2] = tmp;
}
i = j;
}
for (k1 = 0, k2 = i - 1; k1 < k2;k1++, k2--)
{
tmp = str[k1];
str[k1] = str[k2];
str[k2] = tmp;
}
cout << "单词逆序输出:"<<str<<endl ;
system("pause");
return 0;
}