2013-09-15 14:49:09
将一个字符串中的单词进行倒置,标点符号也倒置,
如输入:hello,nice to meet you!
输出:!you meet to nice,hello
将代码中的while ( *pCur && IsAlphabet(*pCur) )
换成while ( *pCur && *pCur!= ' ' )
即可实现翻转单词,标点符号不倒换,
即输入:hello,nice to meet you!
输出:you! meet to hello,nice
注意TestDriver函数中不能定义输入为:
pCHAR srcStrArray[] = {"0123456","i am a good boy!","hello",""};
因为这样的话,每个char*的指针指向的是静态区,静态区的数据就不能改变,在运行时到ReverseString(char *pSrc,size_t begin,size_t end)函数的 *(pStr + index) = *(pSrc + end - index);就会出错,因为该语句试图改变静态区的数据,是不允许的。
这样的错误,类似于下面的:
char *pStr1 = "hello";
char *pStr2[] = "hello";
可以用*pStr2 = ‘a’;改变pStr2指向的字符串,改变后为"aello";
但不可改变pStr1指向的字符串的内容,因此*pStr1 = ‘a’;是不允许的。
代码(测试暂未发现错误,欢迎交流指正!):
1 #include <iostream> 2 #include <cassert> 3 using namespace std; 4 5 //翻转首地址为pSrc,索引从begin到end的字符 6 void ReverseString(char *pSrc,size_t begin,size_t end) 7 { 8 assert(pSrc != NULL); 9 char *pStr = (char *)(pSrc + begin); 10 char tmpChar; 11 size_t index = 0; 12 13 while ( 2 * index < (end - begin) ) 14 { 15 tmpChar = *(pStr + index); 16 *(pStr + index) = *(pSrc + end - index); 17 *(pSrc + end - index) = tmpChar; 18 19 ++index; 20 } 21 } 22 23 //判断字符是否为字母 24 bool IsAlphabet(char ch) 25 { 26 return ( (ch >= 'a' && ch <= 'z') || (ch >= 'A' && ch <= 'Z') ); 27 } 28 29 //翻转单词 30 char * ReverseWord(char *pStr) //不能定义为const 31 { 32 assert(pStr != NULL); 33 size_t len = strlen(pStr); 34 35 if (len == 0) //对空串的处理 36 { 37 return pStr; 38 } 39 40 ReverseString(pStr,0,len - 1); 41 cout<<pStr<<endl; 42 43 char *pCur = (char *)pStr; 44 int wordBegin = 0; 45 int wordEnd = 0; 46 47 while (*pCur) 48 { 49 wordBegin = pCur - pStr; 50 51 while ( *pCur && IsAlphabet(*pCur) ) 52 { 53 ++pCur; 54 } 55 56 wordEnd = pCur - 1 - pStr; //pCur与pStr相等时,wordEnd定义为size_t会出错 57 58 if (wordBegin < wordEnd) 59 { 60 ReverseString(pStr,wordBegin,wordEnd); 61 } 62 63 ++pCur; 64 } 65 return pStr; 66 } 67 68 69 typedef char * pCHAR; 70 71 void TestDriver() 72 { 73 //pCHAR srcStrArray[] = {"0123456","i am a good boy!","hello",""}; 74 char srcStrArray[][100] = {"0123456","i am a good boy!","hello,nice to meet you!",""}; 75 size_t arrayLength = 4; 76 77 pCHAR srcStr; 78 79 for (size_t index = 0;index < arrayLength;++index) 80 { 81 srcStr = srcStrArray[index]; 82 cout<<"the source string is : "<<srcStr<<endl; 83 84 ReverseWord(srcStr); 85 cout<<"the reversed string is : "<<srcStr<<endl<<endl; 86 } 87 } 88 89 int main() 90 { 91 TestDriver(); 92 return 0; 93 }
测试结果:
the source string is : 0123456 6543210 the reversed string is : 6543210 the source string is : i am a good boy! !yob doog a ma i the reversed string is : !boy good a am i the source string is : hello,nice to meet you! !uoy teem ot ecin,olleh the reversed string is : !you meet to nice,hello the source string is : the reversed string is : 请按任意键继续. . .
将代码中的while ( *pCur && IsAlphabet(*pCur) )
换成while ( *pCur && *pCur!= ' ' )
即可实现翻转单词,标点符号不倒换,测试结果:
the source string is : 0123456 6543210 the reversed string is : 0123456 the source string is : i am a good boy! !yob doog a ma i the reversed string is : boy! good a am i the source string is : hello,nice to meet you! !uoy teem ot ecin,olleh the reversed string is : you! meet to hello,nice the source string is : the reversed string is : 请按任意键继续. . .