当前位置:网站首页>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
边栏推荐
- [proteus simulation] automatic range (range < 10V) switching digital voltmeter
- eolink 如何助力远程办公
- Leetcode153 - find the minimum value in the rotation sort array - array - binary search
- 分享3个使用工具,在家剪辑5个作品挣了400多
- JUC学习记录(2022.4.22)
- 【thymeleaf】处理空值和使用安全操作符
- Have you really learned the operation of sequence table?
- 买卖股票的最佳时机系列问题
- How to upload large files quickly?
- Compiling OpenSSL
猜你喜欢
8.3 language model and data set
TLS / SSL protocol details (28) differences between TLS 1.0, TLS 1.1 and TLS 1.2
1n5408-asemi rectifier diode
[stc8g2k64s4] introduction of comparator and sample program of comparator power down detection
asp. Net method of sending mail using mailmessage
Leetcode162 - find peak - dichotomy - array
How to design a good API interface?
Openfaas practice 4: template operation
Redis master-slave synchronization
Leetcode167 - sum of two numbers II - double pointer - bisection - array - Search
随机推荐
The life cycle of key value in redis module programming
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
LeetCode 练习——396. 旋转函数
Introduction to dirty reading, unrepeatable reading and phantom reading
How does eolink help telecommuting
Practice of unified storage technology of oppo data Lake
X509 certificate cer format to PEM format
LeetCode167-两数之和II-双指针-二分-数组-查找
大文件如何快速上传?
C language super complete learning route (collection allows you to avoid detours)
Detailed explanation of C language knowledge points -- data types and variables [1] - carry counting system
Daily question - leetcode396 - rotation function - recursion
TLS / SSL protocol details (30) RSA, DHE, ecdhe and ecdh processes and differences in SSL
Thinkphp5 + data large screen display effect
Flink datastream type system typeinformation
Subnet division of flannel principle
Tun equipment principle
What is the role of the full connection layer?
How to use OCR in 5 minutes
What is the effect of Zhongfu Jinshi wealth class 29800? Walk with professional investors to make investment easier