当前位置:网站首页>[day_020419] inverted string
[day_020419] inverted string
2022-07-26 06:11:00 【On the Bank of Anhe Bridge】
Inverted string
Title source
Cattle from : Inverted string
Title Description
Invert the words of a sentence , Punctuation is not inverted . such as I like beijing. After passing through the function, it becomes :beijing. like I
Input description
Each test input contains 1 Test cases : I like beijing. The length of the input case shall not exceed 100
Output description
Output the inverted string in turn , Split by space
Example 1
Input
I like beijing.
Output
beijing. like I
Thought analysis
- First put the whole sentence upside down , Then reverse word by word
- The outer loop represents the entire string , The inner loop is used to reverse the interval separated by each space , That is, a word
- The inner loop condition is that the current iterator does not point to spaces , And not at the end of the string
- Because inversion is used reverse function , So iterators are used to traverse the string
Code display
#include<iostream>
#include<algorithm>
#include<string>
using namespace std;
int main()
{
string s;
getline(cin,s);
reverse(s.begin(),s.end());
auto start=s.begin();
while(start!=s.end())
{
auto end=start;
// Notice that a space is a character , Use ' ' Single quotation marks
while(*end!=' '&&end!=s.end())
{
end++;
}
// In both cases reverse, So the reverse Let me write it first
reverse(start,end);
// Here, if the judgment condition cannot be written if(*end=' '),
// because end The value at the end of the string is NULL, Dereference will cause segment errors
if(end!=s.end())
{
start=end+1;
}
else
{
start=s.end();
}
}
cout<<s;
return 0;
}
边栏推荐
- A company installs monitoring for each station: write code only to see employees?
- Matlab 向量与矩阵
- Webapi collation
- JS的调用方式与执行顺序
- 【Day_04 0421】进制转换
- 二叉树的前中后序遍历——本质(每个节点都是“根”节点)
- Establishment of log collection and analysis platform-1-environment preparation
- Registration conditions for system integration project management engineer (intermediate level of soft exam) in the second half of 2022
- 移动web
- 数据库sql语言实战
猜你喜欢

Jz36 binary search tree and bidirectional linked list

How to divide the disks under the devices and drives in win10 new computer

Convolutional neural network (III) - target detection

CV (1)- Introduction

Introduction to three feasible schemes of grammatical generalization

【Oracle SQL】计算同比与环比(列转行进行偏移)

Kingbasees SQL language reference manual of Jincang database (8. Function (10))

A company installs monitoring for each station: write code only to see employees?

2022 National latest fire-fighting facility operator (Senior fire-fighting facility operator) simulation test questions and answers
![[(SV & UVM) knowledge points encountered in written interview] ~ phase mechanism](/img/19/32206eb6490c2a5a7a8e746b5003c1.png)
[(SV & UVM) knowledge points encountered in written interview] ~ phase mechanism
随机推荐
Solve vagrant's error b:48:in `join ': incompatible character encodings: GBK and UTF-8 (encoding:: Compatib
对接微信支付(二)统一下单API
JS的调用方式与执行顺序
flex布局
“子问题的递归处理”——判断两棵树是不是相同的树——以及 另一颗树的子树
【Day04_0421】C语言选择题
英语句式参考纯享版 - 定语从句
Mysql45 talks about transaction isolation: why can't I see it after you change it?
Jincang database kingbasees SQL language reference manual (5. Operators)
机械制造企业如何借助ERP系统,做好生产管理?
Recursive processing - subproblem
Should we test the Dao layer?
Easycvr video square channel display and video access full screen display style problem repair
C language explanation series - comprehensive exercises, guessing numbers games
基于消防GIS系统的智慧消防应用
Practice operation and maintenance knowledge accumulation
Convolutional neural network (III) - target detection
2022 National latest fire-fighting facility operator (Senior fire-fighting facility operator) simulation test questions and answers
【Day02_0419】C语言选择题
YOLOv6:又快又准的目标检测框架开源啦