当前位置:网站首页>[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;
}
边栏推荐
- VS中使用动态库
- Meiker Studio - Huawei 14 day Hongmeng equipment development practical notes 4
- 【Day_02 0419】倒置字符串
- Easycvr video square channel display and video access full screen display style problem repair
- Xiao He shows his sharp corners and says hello to flutter app
- 【Day_02 0419】排序子序列
- 实习运维知识积累
- L. Link with Level Editor I dp
- Mobile web
- "Recursive processing of subproblems" -- judging whether two trees are the same tree -- and the subtree of another tree
猜你喜欢

Recursive processing - subproblem

Kingbasees SQL language reference manual of Jincang database (10. Query and sub query)

递归函数中 有两个递归入口的时间复杂度

招标信息获取

基于消防GIS系统的智慧消防应用

Introduction to three feasible schemes of grammatical generalization

1.12 basis of Web Development

Modifiers should be declared in the correct order 修饰符应按正确的顺序声明

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

Niuke network: TOPK problem of additive sum between two ordinal groups
随机推荐
【Day_06 0423】把字符串转换成整数
递归处理——子问题
知识沉淀一:架构师是做什么?解决了什么问题
VRRP protocol and experimental configuration
光量子里程碑:6分钟内解决3854个变量问题
The number of weeks of Oracle last year and this year, with the start time and end time
Establishment of log collection and analysis platform-1-environment preparation
Webapi collation
Print linked list in reverse order
1.12 Web开发基础
【Day_04 0421】计算糖果
Implementation of PHP multitask second timer
ament_ Cmake generates the ros2 library and links it
YOLOv7: Trainable bag-of-freebies sets new state-of-the-art for real-time object detectors
Day110. Shangyitong: gateway integration, hospital scheduling management: Department list, statistics based on date, scheduling details
“子问题的递归处理”——判断两棵树是不是相同的树——以及 另一颗树的子树
Practice operation and maintenance knowledge accumulation
Understanding the mathematical essence of machine learning
unity 像素画导入模糊问题
Latex merges multiple rows and columns of a table at the same time