当前位置:网站首页>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
边栏推荐
- Sword finger offer (2) -- for Huawei
- Introduction to distributed transaction Seata
- January 1, 1990 is Monday. Define the function date_ to_ Week (year, month, day), which realizes the function of returning the day of the week after inputting the year, month and day, such as date_ to
- OPPO数据湖统一存储技术实践
- JUC学习记录(2022.4.22)
- 8.4 realization of recurrent neural network from zero
- Detailed analysis of SQL combat of Niuke database (26-30)
- 封面和标题中的关键词怎么写?做自媒体为什么视频没有播放量
- Differential privacy (background)
- tcp_ Diag kernel related implementation 1 call hierarchy
猜你喜欢

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

Redis master-slave synchronization

My raspberry PI zero 2W toss notes to record some problems and solutions

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

eolink 如何助力远程办公

Introduction to Arduino for esp8266 serial port function
![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]

1n5408-asemi rectifier diode

Thinkphp5 + data large screen display effect

Sword finger offer (1) -- for Huawei
随机推荐
Nacos program connects to mysql8 0+ NullPointerException
Async keyword
MySQL Basics
Is asemi ultrafast recovery diode interchangeable with Schottky diode
LeetCode151-颠倒字符串中的单词-字符串-模拟
Adobe Illustrator menu in Chinese and English
async关键字
Nacos程序连接MySQL8.0+ NullPointerException
X509 certificate cer format to PEM format
Llvm - generate for loop
My raspberry PI zero 2W toss notes to record some problems and solutions
Basic operation of sequential stack
Redis master-slave synchronization
HJ31 单词倒排
Design of digital temperature monitoring and alarm system based on DS18B20 single chip microcomputer [LCD1602 display + Proteus simulation + C program + paper + key setting, etc.]
asp. Net method of sending mail using mailmessage
Async void caused the program to crash
LeetCode149-直线上最多的点数-数学-哈希表
LeetCode153-寻找旋转排序数组中的最小值-数组-二分查找
SQL中HAVING和WHERE的区别