1.方法一:
I am from shanghai ----->shanghai from am I
1. I ma morf iahgnahs
2. shanghai from am I
#include "iostream"
using namespace std;
void RevStr(char * src)
{
char * start=src,*end=src,*ptr=src;
while (*ptr++!='\0') //
{
{end=ptr-1;while (start<end){swap(*start++,*end--);}start=ptr+1;if (*ptr==' '||*ptr=='\0')
}
}
start=src,end=ptr-2;
while(start<end)
swap(*start++,*end--);
}
int main()
{
char src[]={"I am from Shanghai"};
cout<<src<<endl;
RevStr(src);
cout<<src<<endl;
return 0;
}
2.方法二:
1. iahgnahs morf ma I
2. shanghai from am I
#include "iostream"
using namespace std;
void RevStr(char * src)
{
char * start=src,*end=src,*ptr=src;
while (*ptr++!='\0') //
end=ptr-1;
while (start<end)
{
swap(*start++,*end--);
}
start=src,end=ptr-2,ptr=src;
while(*ptr++!='\0')
{
if (*ptr==' '||*ptr=='\0')
{
end=ptr-1;
while(start<end)
swap(*start++,*end--);
start=end=ptr+1;
}
}
}
int main()
{
char src[]={"I am from Shanghai asdfg"};
cout<<src<<endl;
RevStr(src);
cout<<src<<endl;
return 0;
}