给定一句英语,要求你编写程序,将句中所有单词的顺序颠倒输出。
输入格式:测试输入包含一个测试用例,在一行内给出总长度不超过80的字符串。字符串由若干单词和若干空格组成,其中单词是由英文字母(大小写有区分)组成的字符串,单词之间用1个空格分开,输入保证句子末尾没有多余的空格。
输出格式:每个测试用例的输出占一行,输出倒序后的句子。
输入样例:
Hello World Here I Come
输出样例:
Come I Here World Hello
#include <iostream>
#include <vector>
#include <string>
#include <cstring>
using namespace std;
/* run this program using the console pauser or add your own getch, system("pause") or input loop */
int main() {
int i, j;
string word;
string wordx;
vector <string> stc;
getline(cin, word);
for (i = 0,j=0; i < word.length(); i++) {
if (word[i] != ' ') {
wordx += word[i];
}
else {
stc.push_back(wordx);
wordx = {};
}
}
stc.push_back(wordx);
cout<< stc.back();
stc.pop_back();
while(!stc.empty()){
cout<<" "<<stc.back();
stc.pop_back();
}
return 0;
}
想输入想了半天,发现cin>>不能区分空格和换行,遂放弃,想用strtok,但是没有实现。。。