微软100题-010反转字符串

来源:互联网 发布:精解windows 10 pdf 编辑:程序博客网 时间:2024/06/03 20:24

翻转句子中单词的顺序。
题目:输入一个英文句子,翻转句子中单词的顺序,但单词内字符的顺序不变。
句子中单词以空格符隔开。为简单起见,标点符号和普通字母一样处理。
例如输入“I am a student.”,则输出“student. a am I”。

#include<iostream>#include<algorithm>#include<cctype>#include<string>using namespace std;int main(void){    string str;    while (getline(cin, str))    {        reverse(str.begin(), str.end());        int count = 0;        for (int i = 0; i < (int)str.size();++i)            if (isblank(str[i]))            {            reverse(&str[count], &str[i]);            count = i + 1;            }        reverse(&str[count], &str[str.size()]);        cout << str << endl;        str.clear();    }    return 0;}
原创粉丝点击