当前位置:网站首页>Hj31 word inversion
Hj31 word inversion
2022-04-23 15:12:00 【coder_ Alger】
describe
Invert all the words in the string .
explain :
1、 The characters that make up a word are 26 A capital or lowercase letter ;
2、 Characters that do not make up a word are treated as word separators ;
3、 The inverted word spacer is required to be represented by a space ; If there are multiple separators between adjacent words in the original string , Only one space spacer is allowed after inverted conversion ;
4、 Each word is the longest 20 Letters ;
Example 1
Input :
I am a student
Copy output :
student a am I
Copy
Example 2
Input :
$bo*y gi!r#l
Copy output :
l r gi y bo
#include <iostream>
using namespace std;
#include <string>
#include <vector>
int Word_Rev(string str)
{
vector<string>vec;// Use vectors to store words
int len = str.size();
int sublen = 0;// Record the length of each substring
// Split the words in the sentence
for(int i = 0;i<len;i++)
{
if((str[i]>='a'&&str[i]<='z') || (str[i]>='A'&&str[i]<='Z'))
{
sublen++;
continue;
}
else
{
// Be careful : If there is no special character after the last word , I can't get there
if(sublen>0)
{
vec.push_back(str.substr(i-sublen,sublen));
sublen = 0;
}
}
}
// Write the last word to the vector
if(sublen>0)
{
vec.push_back(str.substr(len-sublen,sublen));
}
// Output in reverse order
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://yzsam.com/2022/04/202204231507258849.html
边栏推荐
- Nuxt project: Global get process Env information
- Common interview questions of operating system:
- Reptile exercises (1)
- Borui data and F5 jointly build the full data chain DNA of financial technology from code to user
- Openfaas practice 4: template operation
- redis-shake 使用中遇到的错误整理
- The difference between having and where in SQL
- Role of asemi rectifier module mdq100-16 in intelligent switching power supply
- MySQL sync could not find first log file name in binary log index file error
- eolink 如何助力遠程辦公
猜你喜欢

eolink 如何助力远程办公

Leetcode167 - sum of two numbers II - double pointer - bisection - array - Search

The win10 taskbar notification area icon is missing

免费在upic中设置OneDrive或Google Drive作为图床

让阿里P8都为之着迷的分布式核心原理解析到底讲了啥?看完我惊了

TLS / SSL protocol details (28) differences between TLS 1.0, TLS 1.1 and TLS 1.2

Kubernetes详解(九)——资源配置清单创建Pod实战

LeetCode151-颠倒字符串中的单词-字符串-模拟

Tun equipment principle

Redis主从同步
随机推荐
About UDP receiving ICMP port unreachable
大文件如何快速上传?
Error: unable to find remote key "17f718f726"“
HJ31 单词倒排
Nacos program connects to mysql8 0+ NullPointerException
Baidu written test 2022.4.12 + programming topic: simple integer problem
Design of digital temperature monitoring and alarm system based on DS18B20 single chip microcomputer [LCD1602 display + Proteus simulation + C program + paper + key setting, etc.]
Nuxt project: Global get process Env information
SQLSERVER事物与锁的问题
Difference between like and regexp
Sword finger offer (2) -- for Huawei
Llvm - generate if else and pH
Leetcode151 - invert words in string - String - simulation
My raspberry PI zero 2W tossing notes record some problems encountered and solutions
Kubernetes详解(十一)——标签与标签选择器
小红书 timestamp2 (2022/04/22)
Comparaison du menu de l'illustrateur Adobe en chinois et en anglais
封面和标题中的关键词怎么写?做自媒体为什么视频没有播放量
Basic operation of sequential stack
For 22 years, you didn't know the file contained vulnerabilities?