#include <iostream>
#include <string>
#include <vector>
using namespace std;
string &ConvSent(const string &Sent, string &InvSent)
{
vector <string> words;
size_t i, beg, end;
bool is_word = false;
for (i = 0; i < Sent.length(); ++i)
{
if (Sent[i] != ' ' && is_word == false)
{
is_word = true;
beg = i;
}
else if (Sent[i] == ' ' && is_word == true)
{
is_word = false;
end = i;
words.push_back(Sent.substr(beg, end - beg));
words.push_back(" ");
}
else if (Sent[i] == ' ' && is_word == false)
{
words.push_back(" ");
}
}
if (is_word == true && i == Sent.length())
{
words.push_back(Sent.substr(beg, i - beg));
}
vector <string>::const_reverse_iterator it = words.rbegin();
for ( ; it != words.rend(); ++it)
{
InvSent += *it;
cout << *it << endl;
}
return InvSent;
}
int main()
{
string aSent;
getline(cin, aSent);
string invSent;
cout << ConvSent(aSent, invSent) << endl;
return 0;
}
将一句话里的单词进行逆置,标点符号不逆置
最新推荐文章于 2019-11-09 20:22:14 发布