题目:将一个句子的单词反过来 I am Student ----> Student am I
#include <iostream>
using namespace std;
int main(void)
{
int num=-12345,j=0,i=0,flag=0,begin,end;
char str[]="I am a student",temp;
j=strlen(str)-1;
cout<<"String="<<str<<endl;
//第一步是进行全盘反转,将单词变成“tneduts a ma I”
while(j>i)
{
temp=str[i];
str[i]=str[j];
str[j]=temp;
j--;
i++;
}
cout<<" String="<<str<<endl;
int k=0;
//第二步进行部分反转,如果不是空格则开始反转单词
while(str[k])
{
if(str[k]!=' ')
{
begin=k;
while(str[k]&&str[k]!=' ')
{
k++;
}
k=k-1;
end=k;
}
while(end>begin)
{
temp=str[begin];
str[begin]=str[end];
str[end]=temp;
end--;
begin++;
}
k++;
}
cout<<"String="<<str<<endl;
return 0;
}