翻转单词顺序(两次旋转)

来源:互联网 发布:淘宝客服工作视频教程 编辑:程序博客网 时间:2024/05/21 17:35

//字符串翻转:I am a student. 变换为 student. a am I

void Reverse(char *pBegin, char *pEnd){if (pBegin == NULL || pEnd == NULL)return;while (pBegin < pEnd){char temp = *pBegin;*pBegin = *pEnd;*pEnd = temp;pBegin++;pEnd--;}}char *ReverseSentence(char *pData){if (pData == NULL)return NULL;char *pBegin = pData;char *pEnd = pData;while (*pEnd != '\0')pEnd++;pEnd--;Reverse(pBegin, pEnd);pBegin = pEnd = pData;while (*pBegin != '\0'){if (*pBegin == ' '){pBegin++;pEnd++;}else if (*pEnd == ' ' || *pEnd == '\0'){Reverse(pBegin, --pEnd);pBegin = ++pEnd;}elsepEnd++;}return pData;}


原创粉丝点击