将一句话里的单词进行倒置,标点符号不倒换(c++)

来源:互联网 发布:淘宝客高佣金计划 编辑:程序博客网 时间:2024/05/16 06:18

实现一个函数将一句话里的单词进行倒置,标点符号不倒换。比如一句话“i come from wuhan.“倒置后变成"wuhan. from come i"。

#pragma warning (disable:4786)
#include <iostream>
#include<stack>
#include<string>
#include <sstream>

using namespace std;

int main(void)
{
    stack<string> sstack;
    string line, word;

    getline(cin, line);

    istringstream stream(line);//字符输入流
    while(stream >> word)
    {
        sstack.push(word);
    }

    while(!sstack.empty())
    {
        cout << sstack.top() << " ";
        sstack.pop();
    }

    cout << endl;
    return 0;    
}

问题:VC6.0下getline需要两次才能打印结果,和初衷想法回车即打印不同!应该是VC6.0,在VS和linux应该不会出现这样的问题。解决方法:

(1)建立一个1.CPP

(2)输入#include <string>

(3)右击<string>,选择“打开文档<string>”

(4)用CTRL+F查找 else if (_Tr::eq((_E)_C, _D))

1 else if (_Tr::eq((_E)_C, _D))2            {_Chg = true;3              _I.rdbuf()->snextc();4             break; } 改:
1 else if (_Tr::eq((_E)_C, _D))2         {_Chg = true;3         //  _I.rdbuf()->snextc(); 4         // (this comments out the defective instruction) 5         _I.rdbuf()->sbumpc(); // corrected code 6         break; } 
复制代码
保存退出后即可修复这个问题。
原创粉丝点击