标签:
其实翻转字符串能够通过多次的字符串的逆序实现,这里主要学习一下,怎么提取每一个英语单词,然后分别对其逆序。
string ReverseSentence(string str) { int size = str.size(); char *pFirst = &str[0]; char *pEnd = &str[size - 1]; Reverse(pFirst, pEnd); pFirst = &str[0]; pEnd = pFirst; while(*pFirst != ‘\0‘) { if(*pFirst == ‘ ‘) { ++pFirst; ++pEnd; } else if(*pEnd == ‘ ‘ || *pEnd == ‘\0‘) { Reverse(pFirst, --pEnd); pFirst = ++pEnd; } else { ++pEnd; } }//for return str; } void Reverse(char *pBegin, char *pEnd) { if(pBegin == NULL || pEnd == NULL) { return; } while(pBegin < pEnd) { char tmp = *pBegin; *pBegin = *pEnd; *pEnd = tmp; pBegin++; pEnd--; }//while }
标签:
原文地址:http://www.cnblogs.com/stemon/p/4811829.html