如"i am a student" 倒置为 "student a am i"
思路:
1)倒置整个字符串
2)倒置每个单词
#include <iostream> #include <string.h> using namespace std; void Reverse_word(char *p,char *q){ if (p==q) { return; } while (p<q) { char tmp=*p; *p=*q; *q=tmp; q--; p++; } } void Reverse_sentance(char pSentance[]){ if (pSentance==NULL) { return; } Reverse_word(pSentance,pSentance+strlen(pSentance)-1); char *pBegin=pSentance; char *pEnd=pSentance; while (*pEnd!='\0') { if (*pEnd!=' ') { pEnd++; }else{ Reverse_word(pBegin,pEnd-1); pBegin=++pEnd; } } Reverse_word(pBegin,pEnd-1);//逆转最后一个词 } int main(){ char p[]="i am a student"; printf("%s\n",p); Reverse_sentance(p); printf("%s\n",p); return 0; }原文链接:/javaschema/285204.html