当前位置:网站首页>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
边栏推荐
- Comparaison du menu de l'illustrateur Adobe en chinois et en anglais
- Byte interview programming question: the minimum number of K
- Flink datastream type system typeinformation
- Tun model of flannel principle
- Openfaas practice 4: template operation
- X509 certificate cer format to PEM format
- 填充每个节点的下一个右侧节点指针 II [经典层次遍历 | 视为链表 ]
- How to use OCR in 5 minutes
- Sqlserver transaction and lock problem
- Differential privacy (background)
猜你喜欢

eolink 如何助力远程办公

博睿数据携手F5共同构建金融科技从代码到用户的全数据链DNA
![Detailed explanation of C language knowledge points -- data types and variables [1] - carry counting system](/img/95/3b38a550e78b3467c4a756af073d0a.png)
Detailed explanation of C language knowledge points -- data types and variables [1] - carry counting system

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

Daily question - leetcode396 - rotation function - recursion

大文件如何快速上传?
![[stc8g2k64s4] introduction of comparator and sample program of comparator power down detection](/img/8c/e72e628a44a36bfc7406a002d00215.png)
[stc8g2k64s4] introduction of comparator and sample program of comparator power down detection

Share 20 tips for ES6 that should not be missed

Tun equipment principle

Basic operation of sequential stack
随机推荐
Detailed explanation of C language knowledge points -- first understanding of C language [1] - vs2022 debugging skills and code practice [1]
1990年1月1日是星期一,定义函数date_to_week(year,month,day),实现功能输入年月日后返回星期几,例如date_to_week(2020,11,1),返回:星期日。 提示:
Difference between like and regexp
async void 导致程序崩溃
The wechat applet optimizes the native request through the promise of ES6
Thinkphp5 + data large screen display effect
Role of asemi rectifier module mdq100-16 in intelligent switching power supply
Nacos程序连接MySQL8.0+ NullPointerException
Basic operation of sequential stack
JUC学习记录(2022.4.22)
小红书 timestamp2 (2022/04/22)
牛客网数据库SQL实战详细剖析(26-30)
Llvm - generate addition
Byte interview programming question: the minimum number of K
SQLSERVER事物与锁的问题
Comparaison du menu de l'illustrateur Adobe en chinois et en anglais
SQL中HAVING和WHERE的区别
让阿里P8都为之着迷的分布式核心原理解析到底讲了啥?看完我惊了
Share 3 tools, edit 5 works at home and earn more than 400
Pnpm installation and use