您的位置:首页 > 其它

华为OJ中级题-单词倒排

2016-01-28 14:36 344 查看
题目描述

对字符串中的所有单词进行倒排。

说明:

1、每个单词是以26个大写或小写英文字母构成;

2、非构成单词的字符均视为单词间隔符;

3、要求倒排后的单词间隔符以一个空格表示;如果原字符串中相邻单词间有多个间隔符时,倒排转换后也只允许出现一个空格间隔符;

4、每个单词最长20个字母;

样例输入

I am a student

样例输入

student a am I

void HWoj(){
string str,dst;
vector<string> dstOut;
getline(cin, str);
int len = str.length();
for (int i = 0; i < len; ++i){
if (str[i] != ' '){
dst.push_back(str[i]);
}
else{
dstOut.push_back(dst);
dst.clear();
}
}
dstOut.push_back(dst);
reverse(dstOut.begin(), dstOut.end());
len = dstOut.size();
for (int i = 0; i < len; ++i){
cout << dstOut.at(i) << " ";
}
cout << endl;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: