【字符串问题】将一个字符串中的单词进行倒置
2013-09-15 14:58
423 查看
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’;是不允许的。
代码(测试暂未发现错误,欢迎交流指正!):
测试结果:
将代码中的while ( *pCur && IsAlphabet(*pCur) )
换成while ( *pCur && *pCur!= ' ' )
即可实现翻转单词,标点符号不倒换,测试结果:
将一个字符串中的单词进行倒置,标点符号也倒置,
如输入: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’;是不允许的。
代码(测试暂未发现错误,欢迎交流指正!):
#include <iostream> #include <cassert> using namespace std; //翻转首地址为pSrc,索引从begin到end的字符 void ReverseString(char *pSrc,size_t begin,size_t end) { assert(pSrc != NULL); char *pStr = (char *)(pSrc + begin); char tmpChar; size_t index = 0; while ( 2 * index < (end - begin) ) { tmpChar = *(pStr + index); *(pStr + index) = *(pSrc + end - index); *(pSrc + end - index) = tmpChar; ++index; } } //判断字符是否为字母 bool IsAlphabet(char ch) { return ( (ch >= 'a' && ch <= 'z') || (ch >= 'A' && ch <= 'Z') ); } //翻转单词 char * ReverseWord(char *pStr) //不能定义为const { assert(pStr != NULL); size_t len = strlen(pStr); if (len == 0) //对空串的处理 { return pStr; } ReverseString(pStr,0,len - 1); cout<<pStr<<endl; char *pCur = (char *)pStr; int wordBegin = 0; int wordEnd = 0; while (*pCur) { wordBegin = pCur - pStr; while ( *pCur && IsAlphabet(*pCur) ) { ++pCur; } wordEnd = pCur - 1 - pStr; //pCur与pStr相等时,wordEnd定义为size_t会出错 if (wordBegin < wordEnd) { ReverseString(pStr,wordBegin,wordEnd); } ++pCur; } return pStr; } typedef char * pCHAR; void TestDriver() { //pCHAR srcStrArray[] = {"0123456","i am a good boy!","hello",""}; char srcStrArray[][100] = {"0123456","i am a good boy!","hello,nice to meet you!",""}; size_t arrayLength = 4; pCHAR srcStr; for (size_t index = 0;index < arrayLength;++index) { srcStr = srcStrArray[index]; cout<<"the source string is : "<<srcStr<<endl; ReverseWord(srcStr); cout<<"the reversed string is : "<<srcStr<<endl<<endl; } } int main() { TestDriver(); return 0; }
测试结果:
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 : 请按任意键继续. . .
相关文章推荐
- (1)写一个程序,用于分析一个字符串中各个单词出现的频率,并将单词和它出现的频率输出显示。(单词之间用空格隔开,如“Hello World My First Unit Test”); (2)编写单元测试进行测试; (3)用ElcEmma查看代码覆盖率,要求覆盖率达到100%。
- 【字符串操作】13.将一句话单词进行倒置
- 两个字符串hash问题:检查两个单词是否互为变位词和在一个字符串中第一个只出现一次的字符
- 字符串---将一句话里的单词进行倒置,标点符号不倒换
- 一个汉字作为一个字符进行字符串长度计算和切割
- 将一句话中单词进行倒置,标点符号不倒置
- 写一个函数对字符串数组进行排序,排序的规则是根据每个字符串中……
- 一个看似简单却复杂的问题:求两个字符串的 左向右匹配 所有的 最长连续的 公共子字符串( 在每个字符串中先后次序相同的) 序列
- 计算出一字符串(字符串中每个单词之间有一个或多个空格)中每个单词的 出现的次数
- 3.java把一个数组int[]array={1,2,3,4,5,6,7}中的元素进行倒置,变成int[]array={7,6,5,4,3,2,1}.
- 字符串面试题:将句子的单词序倒置
- VC中一个关于宏的使用问题,字符串之间转换,宽字符与普通字符
- mysql创建表时列类型设置为字符串类型的一个小问题
- 编写一个C函数,将”I am from shanghai ”倒置为”shanghai from am I”,及将句子中的单词位置倒置,而不改变单词内部结构.
- 将一个十六进制字符串转换为十进制数值的问题|| 以及C++中string转换成char*|| c++文件按行输入
- 3.7 按升序对一个字符串栈进行排序
- 写一个函数,要求输入一个字符串和一个字符长度,对该字符串进行分 隔。
- 将字符串中的每个单词顺序进行颠倒,单词还是原来的单词,字母顺序不发生变化
- 一个字符串既含有字母又含有数字且字母和数字随意排列如何取出其中所有的数字并进行排序
- win7问题解决,凭据管理器和无法访问,不允许一个用户使用一个以上用户名与服务器或共享资源进行多重连接。