将字符串中单词逆置后输出
#include<iostream>using namespace std;
int main()
{
//整体翻转
char a[]="how are you";
char *p1,*p2;
p1=p2=a;
while(*p2!='\0')
{
p2++;
}
p2--;
while(p1<=p2)
{
char k;
k=*p1;
*p1=*p2;
*p2=k;
p1++;p2--;
}
//单词翻转
p1=p2=a;
while(*p2!='\0')
{
if(*p2==' ')
{
char *p;
p=p2-1;
while(p1<=p)
{
char m;
m=*p1;*p1=*p;*p=m;
p1++;p--;
}
p1=p2+1;
}
else
p2++;
}
p2--;
while(p1<=p2)
{
char w;
w=*p1;*p1=*p2;*p2=w;
p1++;p2--;
}
puts(a);
system("pause");
return 0;
}
例如:how are you变成you are how