当前位置:网站首页>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
边栏推荐
- Redis主从同步
- For 22 years, you didn't know the file contained vulnerabilities?
- 调度系统使用注意事项
- Application of skiplist in leveldb
- PSYNC synchronization of redis source code analysis
- 8.2 text preprocessing
- How does eolink help telecommuting
- UML学习_day2
- API gateway / API gateway (III) - use of Kong - current limiting rate limiting (redis)
- API gateway / API gateway (II) - use of Kong - load balancing
猜你喜欢
Detailed explanation of C language knowledge points - data types and variables [2] - integer variables and constants [1]
1n5408-asemi rectifier diode
Thinkphp5 + data large screen display effect
分布式事务Seata介绍
Five data types of redis
Differential privacy (background)
Tencent has written a few words, Ali has written them all for a month
Have you learned the basic operation of circular queue?
UML learning_ Day2
setcontext getcontext makecontext swapcontext
随机推荐
How to design a good API interface?
MySQL Basics
C language super complete learning route (collection allows you to avoid detours)
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
Llvm - generate local variables
我的树莓派 Raspberry Pi Zero 2W 折腾笔记,记录一些遇到的问题和解决办法
nuxt项目:全局获取process.env信息
Alexnet model
MySQL installation process (steps for successful installation)
async void 导致程序崩溃
A series of problems about the best time to buy and sell stocks
Kubernetes详解(九)——资源配置清单创建Pod实战
中富金石财富班29800效果如何?与专业投资者同行让投资更简单
The win10 taskbar notification area icon is missing
Thinkphp5 + data large screen display effect
分布式事务Seata介绍
Sword finger offer (1) -- for Huawei
How to write the keywords in the cover and title? As we media, why is there no video playback
Llvm - generate addition
On the day of entry, I cried (mushroom street was laid off and fought for seven months to win the offer)