描述
对字符串中的所有单词进行倒排。
说明:
1、构成单词的字符只有26个大写或小写英文字母;
2、非构成单词的字符均视为单词间隔符;
3、要求倒排后的单词间隔符以一个空格表示;如果原字符串中相邻单词间有多个间隔符时,倒排转换后也只允许出现一个空格间隔符;
4、每个单词最长20个字母;
数据范围:字符串长度满足 1≤n≤10000 1≤n≤10000
输入描述:
输入一行,表示用来倒排的句子
输出描述:
输出句子的倒排结果
示例1
输入:
I am a student
输出:
student a am I
示例2
输入:
$bo*y gi!r#l
输出:
l r gi y bo
#include<bits/stdc++.h>
#include <cstdio>
using namespace std;
int main()
{char c;string word;string ans;while(cin.get(c)){if(isalpha(c)){word=word+c;}if(!isalpha(c)){//不存在if(word.empty()){continue;}//存在wordif(!word.empty()){ans=word+' '+ans;word.clear();}}}cout<<ans;
}
小课堂
逐个输入字符的方法
while(cin.getchar(c))
字符串可以相加,可以加到前面或后面
string str;
string str2;
string str3;
str3=str1+str2;
str3=str2+str1;
判断字符是否为大写或者小写字母
isalpha(c)
!isalpha(c)
清空字符串
str.clear();