您的位置:首页 > 其它

【字符串问题】将一个字符串中的单词进行倒置

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’;是不允许的。

代码(测试暂未发现错误,欢迎交流指正!):

#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 :

请按任意键继续. . .
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: 
相关文章推荐