当前位置:网站首页>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
边栏推荐
猜你喜欢

冰冰学习笔记:一步一步带你实现顺序表

Analysis of common storage types and FTP active and passive modes

LeetCode153-寻找旋转排序数组中的最小值-数组-二分查找

For 22 years, you didn't know the file contained vulnerabilities?

MySQL error packet out of order
![Detailed explanation of C language knowledge points -- first understanding of C language [1] - vs2022 debugging skills and code practice [1]](/img/07/c534238c2b5405bbe4655e51cfee51.png)
Detailed explanation of C language knowledge points -- first understanding of C language [1] - vs2022 debugging skills and code practice [1]

Leetcode162 - find peak - dichotomy - array

Programming philosophy - automatic loading, dependency injection and control inversion

nuxt项目:全局获取process.env信息

博睿数据携手F5共同构建金融科技从代码到用户的全数据链DNA
随机推荐
Practice of unified storage technology of oppo data Lake
C语言超全学习路线(收藏让你少走弯路)
Explain TCP's three handshakes in detail
Redis cluster principle
Reptile exercises (1)
The difference between having and where in SQL
Detailed explanation of C language knowledge points -- first understanding of C language [1] - vs2022 debugging skills and code practice [1]
JS -- realize click Copy function
Leetcode167 - sum of two numbers II - double pointer - bisection - array - Search
Basic operation of sequential stack
Openfaas practice 4: template operation
Detailed explanation of C language knowledge points - data types and variables [2] - integer variables and constants [1]
Five data types of redis
SSH connects to the remote host through the springboard machine
tcp_ Diag kernel related implementation 1 call hierarchy
LeetCode162-寻找峰值-二分-数组
Advanced version of array simulation queue - ring queue (real queuing)
LeetCode167-两数之和II-双指针-二分-数组-查找
Introduction to dirty reading, unrepeatable reading and phantom reading
My raspberry PI zero 2W tossing notes record some problems encountered and solutions