#include <iostream>
#include <string.h>
using namespace std;
void reverse_word(char *pBegin, char *pEnd)
{
if (pBegin == NULL || pEnd == NULL)
return;
while (pBegin<pEnd)
{
swap(*pBegin, *pEnd);
pBegin++;
pEnd--;
}
}
void reverse_sentence(char *str)
{
char *pBegin = str;
char *pEnd = str;
while (*pBegin != '\0')
{
if (*pBegin == ' ')
{
pBegin++;
pEnd++;
}
else if (*pEnd == ' ' || *pEnd == '\0')
{
reverse_word(pBegin, --pEnd);
pBegin = ++pEnd;
}
else
{
pEnd++;
}
}
}
int main()
{
char str[] = "i am a student in neu";
reverse_word(str,str+strlen(str)-1);
reverse_sentence(str);
cout << str << endl;
return 0;
}
翻转单词顺序
最新推荐文章于 2020-04-28 09:28:06 发布