当前位置:网站首页>HJ31 单词倒排
HJ31 单词倒排
2022-04-23 15:07:00 【coder_Alger】
描述
对字符串中的所有单词进行倒排。
说明:
1、构成单词的字符只有 26 个大写或小写英文字母;
2、非构成单词的字符均视为单词间隔符;
3、要求倒排后的单词间隔符以一个空格表示;如果原字符串中相邻单词间有多个间隔符时,倒排转换后也只允许出现一个空格间隔符;
4、每个单词最长 20 个字母;
示例 1
输入:
I am a student
复制输出:
student a am I
复制
示例 2
输入:
$bo*y gi!r#l
复制输出:
l r gi y bo
#include <iostream>
using namespace std;
#include <string>
#include <vector>
int Word_Rev(string str)
{
vector<string>vec;//用向量来存储单词
int len = str.size();
int sublen = 0;//记录每个子字符串的长度
//将句子中的单词分割
for(int i = 0;i<len;i++)
{
if((str[i]>='a'&&str[i]<='z') || (str[i]>='A'&&str[i]<='Z'))
{
sublen++;
continue;
}
else
{
//注意:若最后一个单词后面没有特殊字符,进入不到这步
if(sublen>0)
{
vec.push_back(str.substr(i-sublen,sublen));
sublen = 0;
}
}
}
//将最后一个单词写入向量
if(sublen>0)
{
vec.push_back(str.substr(len-sublen,sublen));
}
//倒序输出
for(vector<string>::reverse_iterator it = vec.rbegin();it != vec.rend();it++)
{
cout<<*it<<" ";
}
cout<<endl;
return 0;
}
int main()
{
string str;
while(getline(cin,str))
{
Word_Rev(str);
}
return 0;
}
版权声明
本文为[coder_Alger]所创,转载请带上原文链接,感谢
https://blog.csdn.net/weixin_42289227/article/details/124361023
边栏推荐
- The life cycle of key value in redis module programming
- JUC learning record (2022.4.22)
- C language super complete learning route (collection allows you to avoid detours)
- SQLSERVER事物与锁的问题
- Sword finger offer II 019 Delete at most one character to get palindrome (simple)
- Do (local scope), initializer, memory conflict, swift pointer, inout, unsafepointer, unsafebitcast, success
- Share 20 tips for ES6 that should not be missed
- 分享 20 个不容错过的 ES6 的技巧
- Mds55-16-asemi rectifier module mds55-16
- Nacos程序连接MySQL8.0+ NullPointerException
猜你喜欢
Advanced version of array simulation queue - ring queue (real queuing)
分享 20 个不容错过的 ES6 的技巧
Mysql连接查询详解
如何设计一个良好的API接口?
Openfaas practice 4: template operation
Daily question - leetcode396 - rotation function - recursion
nuxt项目:全局获取process.env信息
LeetCode151-颠倒字符串中的单词-字符串-模拟
LeetCode149-直线上最多的点数-数学-哈希表
Leetcode162 - find peak - dichotomy - array
随机推荐
About UDP receiving ICMP port unreachable
Explanation and example application of the principle of logistic regression in machine learning
Set up an AI team in the game world and start the super parametric multi-agent "chaos fight"
JS -- realize click Copy function
The life cycle of key value in redis module programming
setcontext getcontext makecontext swapcontext
Application of skiplist in leveldb
nuxt项目:全局获取process.env信息
[proteus simulation] automatic range (range < 10V) switching digital voltmeter
Role of asemi rectifier module mdq100-16 in intelligent switching power supply
Advanced application of I / O multiplexing: Processing TCP and UDP services at the same time
js——實現點擊複制功能
[stc8g2k64s4] introduction of comparator and sample program of comparator power down detection
Advanced version of array simulation queue - ring queue (real queuing)
Leetcode153 - find the minimum value in the rotation sort array - array - binary search
UML learning_ Day2
How to use OCR in 5 minutes
Error: unable to find remote key "17f718f726"“
OC to swift conditional compilation, marking, macro, log, version detection, expiration prompt
What is the role of the full connection layer?