字串逆序

code

#include<iostream>
#include<string>
#include<algorithm>
using namespace std;
int main(){
	ios::sync_with_stdio(false);
	string str;
	getline(cin,str);
	reverse(str.begin(),str.end());
	cout<<str;
	return 0;
}

tip

使用 cin 和 >> 运算符来输入字符串,但它可能会导致传递并忽略任何前导白色空格字符(空格、制表符或换行符)
为了解决这个问题,可以使用一个叫做 getline 的 C++ 函数。此函数可读取整行,包括前导和嵌入的空格,并将其存储在字符串对象中。


posted @ 2022-02-07 10:00  ethon-wang  阅读(23)  评论(0编辑  收藏  举报