当前位置:网站首页>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
边栏推荐
猜你喜欢
Bingbing learning notes: take you step by step to realize the sequence table
Have you really learned the operation of sequence table?
asp. Net method of sending mail using mailmessage
How to design a good API interface?
Lotus DB design and Implementation - 1 Basic Concepts
How does eolink help telecommuting
8.3 language model and data set
TLS / SSL protocol details (30) RSA, DHE, ecdhe and ecdh processes and differences in SSL
Mds55-16-asemi rectifier module mds55-16
Tun equipment principle
随机推荐
Thread synchronization, life cycle
C language super complete learning route (collection allows you to avoid detours)
Tun model of flannel principle
如何设计一个良好的API接口?
Go basic reflection
Provided by Chengdu control panel design_ It's detailed_ Introduction to the definition, compilation and quotation of single chip microcomputer program header file
For 22 years, you didn't know the file contained vulnerabilities?
LeetCode 练习——396. 旋转函数
API gateway / API gateway (III) - use of Kong - current limiting rate limiting (redis)
每日一题-LeetCode396-旋转函数-递推
Select receives both normal data and out of band data
TLS / SSL protocol details (28) differences between TLS 1.0, TLS 1.1 and TLS 1.2
Leetcode149 - maximum number of points on a line - Math - hash table
博睿数据携手F5共同构建金融科技从代码到用户的全数据链DNA
How to upload large files quickly?
async关键字
Difference between like and regexp
PSYNC synchronization of redis source code analysis
8.4 realization of recurrent neural network from zero
win10 任务栏通知区图标不见了