当前位置:网站首页>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
边栏推荐
- My raspberry PI zero 2W tossing notes record some problems encountered and solutions
- Thinkphp5 + data large screen display effect
- Borui data and F5 jointly build the full data chain DNA of financial technology from code to user
- Detailed explanation of C language knowledge points - data types and variables [2] - integer variables and constants [1]
- Nacos程序连接MySQL8.0+ NullPointerException
- [NLP] HMM hidden Markov + Viterbi word segmentation
- How does eolink help telecommuting
- Redis cluster principle
- TLS / SSL protocol details (30) RSA, DHE, ecdhe and ecdh processes and differences in SSL
- 1990年1月1日是星期一,定义函数date_to_week(year,month,day),实现功能输入年月日后返回星期几,例如date_to_week(2020,11,1),返回:星期日。 提示:
猜你喜欢
What is the effect of Zhongfu Jinshi wealth class 29800? Walk with professional investors to make investment easier
Thinkphp5 + data large screen display effect
Openfaas practice 4: template operation
Lotus DB design and Implementation - 1 Basic Concepts
[stc8g2k64s4] introduction of comparator and sample program of comparator power down detection
UML learning_ Day2
8.2 text preprocessing
API gateway / API gateway (III) - use of Kong - current limiting rate limiting (redis)
Leetcode162 - find peak - dichotomy - array
On the day of entry, I cried (mushroom street was laid off and fought for seven months to win the offer)
随机推荐
The win10 taskbar notification area icon is missing
LeetCode151-颠倒字符串中的单词-字符串-模拟
What is the role of the full connection layer?
Is asemi ultrafast recovery diode interchangeable with Schottky diode
如何设计一个良好的API接口?
async关键字
Modify the default listening IP of firebase emulators
Provided by Chengdu control panel design_ It's detailed_ Introduction to the definition, compilation and quotation of single chip microcomputer program header file
Llvm - generate for loop
Flink datastream type system typeinformation
How does eolink help telecommuting
Swift protocol Association object resource name management multithreading GCD delay once
Difference between like and regexp
Mds55-16-asemi rectifier module mds55-16
Epolloneshot event of epoll -- instance program
Thinkphp5 + data large screen display effect
牛客网数据库SQL实战详细剖析(26-30)
My raspberry PI zero 2W tossing notes record some problems encountered and solutions
How to upload large files quickly?
Reptile exercises (1)