当前位置:网站首页>【Day_02 0419】倒置字符串
【Day_02 0419】倒置字符串
2022-07-26 06:08:00 【安河桥畔】
倒置字符串
题目来源
牛客网:倒置字符串
题目描述
将一句话的单词进行倒置,标点不倒置。比如 I like beijing. 经过函数后变为:beijing. like I
输入描述
每个测试输入包含1个测试用例: I like beijing. 输入用例长度不超过100
输出描述
依次输出倒置之后的字符串,以空格分割
示例1
输入
I like beijing.
输出
beijing. like I
思路分析
- 先将一句话整体逆置,再逐个单词逆置
- 外层循环表示整个字符串,内层循环用于逆置每一个空格隔开的区间,即一个单词
- 内层循环条件为当前迭代器不指向空格,且不是字符串末尾
- 因为逆置要使用reverse函数,所以采用迭代器对字符串进行遍历
代码展示
#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;
//注意空格是一个字符,要用' '单引号
while(*end!=' '&&end!=s.end())
{
end++;
}
//两种情况都要进行reverse,所以把reverse写在最前面
reverse(start,end);
//此处如果判断条件不能写成if(*end=' '),
//因为end到字符串末尾时值为NULL,解引用会造成段错误
if(end!=s.end())
{
start=end+1;
}
else
{
start=s.end();
}
}
cout<<s;
return 0;
}
边栏推荐
- Flex layout
- Leetcode:934. The shortest Bridge
- Recursive processing - subproblem
- VRRP protocol and experimental configuration
- 语法泛化三种可行方案介绍
- Xiao He shows his sharp corners and says hello to flutter app
- [the most complete and detailed] ten thousand words explanation: activiti workflow engine
- 软件测试面试题全网独家没有之一的资深测试工程师面试题集锦
- 时序动作定位 | 用于弱监督时态动作定位的细粒度时态对比学习(CVPR 2022)
- 金仓数据库 KingbaseES SQL 语言参考手册 (6. 表达式)
猜你喜欢

Excitation method and excitation voltage of hand-held vibrating wire vh501tc acquisition instrument

Implementation of PHP multitask second timer

Introduction of four redis cluster schemes + comparison of advantages and disadvantages

Interview difficulties: difficulties in implementing distributed session, this is enough!

Mysql45 speak in simple terms index

Matlab vector and matrix

Leetcode:934. The shortest Bridge

Redis sentinel cluster setup

Understanding the mathematical essence of machine learning

Kingbasees SQL language reference manual of Jincang database (7. Conditional expression)
随机推荐
【BM2 链表内指定区间反转】
Kingbasees SQL language reference manual of Jincang database (7. Conditional expression)
flex布局
Modifiers should be declared in the correct order 修饰符应按正确的顺序声明
Introduction of four redis cluster schemes + comparison of advantages and disadvantages
实习运维知识积累
数据库sql语言实战
Kingbasees SQL language reference manual of Jincang database (11. SQL statement: abort to alter index)
Acquisition of bidding information
Operating steps for uninstalling the mobile app
金仓数据库 KingbaseES SQL 语言参考手册 (8. 函数(十一))
Flex layout
Easycvr video square channel display and video access full screen display style problem repair
Viewing the technology stack of distributed system from the crash report of station B
Calling mode and execution sequence of JS
K. Link with Bracket Sequence I dp
二叉树的性质 ~
金仓数据库 KingbaseES SQL 语言参考手册 (8. 函数(十))
Can you make a JS to get the verification code?
Mysql45 talking about infrastructure: how is an SQL query executed?