首页 > 技术文章 > 字符串反转----将this is good 转化为good is this 输出。

593213556wuyubao 2014-10-07 10:16 原文

思路:现将整个字符串反转,再将每个单词反转;

#include "iostream" #include "cstring" using namespace std; void reverse_word(char*p,char*q){ char temp; while(q>p){ temp=*p; *p=*q; *q=temp; q--; p++; } } char* indexofnoletter(char*p){ char*t=p; while(*t!=' '&&*t!='\0') t++; return --t; } void main(){ char a[]="this is good"; puts(a); reverse_word(a,a+strlen(a)-1); char*p,*q; p=a; do{ q=indexofnoletter(p); reverse_word(p,q); p=q+2; }while(p<=a+strlen(a)-1); puts(a); }

 

推荐阅读