您的位置:首页 > 其它

字符串中单词的翻转

2013-10-25 13:04 190 查看
#include<iostream>
using namespace std;

void swap (char *chDest, char *chSrc)
{
char temp;
temp = *chDest;
*chDest = *chSrc;
*chSrc = temp;
}
void reverseStr(char *Str)
{
char *head = Str;
char *tail = Str;
char *ptr = Str;

//翻转每个单词
while (*ptr != '\0')
{
ptr++;
if (*ptr == ' ' || *ptr == '\0')
{
tail = ptr-1;
while (head < tail)
swap(head++,tail--);
head = tail = ptr+1;
}
}

//翻转整个字符串
head = Str;
tail = ptr-1;
while (head < tail)
swap(head++,tail--);
}

int main()
{
char string[100];
cin.getline(string,100);
reverseStr(string);
cout << string;

cin.get();
cin.get();
return 0;
}

运行结果:

内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: