比如一句话“I Love China!”转换后变成“China! Love I”.
#include<iostream> using namespace std; void wordsTranspose(char *src); void transpose( char *src,int begin,int end); int main(void) { char src[18]="I Love China!"; cout<<"原字符串:"<<src<<endl; wordsTranspose(src); cout<<"单词倒置后字符串:"<<src<<endl; getchar(); } //将src字符串的src[begin]到src[end]之间的所有字母倒置 void transpose( char *src,int begin,int end) { int i,j,temp; i=begin; j=end; while(i<j) { temp=src[i]; src[i]=src[j]; src[j]=temp; i++; j--; } } void wordsTranspose(char *src) { int begin=0; int end=strlen(src)-1; transpose(src,begin,end); cout<<"全部倒置后字符串:"<<src<<endl; int i,j; i=begin; while(src[i])//以空格为标志,对src的每个单词倒置 { while(src[i]!=' '&&src[i]) { i++; } end=i-1; transpose(src,begin,end); i++; begin=i; } }
运行结果: