*
* reverse用法
<https://blog.csdn.net/qq_40828914/article/details/81138117#reverse用法>
* 1.reverse函数反转string
<https://blog.csdn.net/qq_40828914/article/details/81138117#1reverse函数反转string>
* 2.reverse函数反转字符数组
<https://blog.csdn.net/qq_40828914/article/details/81138117#2reverse函数反转字符数组>
* 例子: <https://blog.csdn.net/qq_40828914/article/details/81138117#例子>
* 分析与解答: <https://blog.csdn.net/qq_40828914/article/details/81138117#分析与解答>


reverse用法

1.reverse函数反转string
#include <iostream> #include <algorithm> #include <string> using namespace std;
int main() { string N; cin>>N; reverse(N.begin(), N.end()); cout<<N<<endl; }
2.reverse函数反转字符数组
#include<iostream> #include<cstring> #include<algorithm> using namespace std;
int main() { char a[101]; cin.getline(a,sizeof(a)); int m=strlen(a);
reverse(a,a+m);puts(a); }
例子:

题目描述


小明终于等来了好朋友的来信,但打开来信就傻眼了,居然看不懂,原来好朋友把信中的每个单词都翻转了顺序,例如“Happy”在朋友的信里变成了“yppaH”。请你编写程序帮助小明还原来信内容吧。

输入
输入一个字符串,以回车结束。该字符串长度不超过100,包含多个单词,单词之间有一个或多个空格。

输出
输出一行,即单词翻转后的字符串,注意每对单词之间的空格数量保持不变。

样例输入
yppaH wen raey
样例输出
Happy new year

分析与解答:

1.遇到空格直接输出
2.遇到字符,把单词存入一个数组里,反转后输出这个单词
#include<iostream> #include<sstream> #include<cstring> #include<string>
#include<algorithm> using namespace std; int main(){ char a[10001]; char b[10001
];cin.getline(a,sizeof(a)); int m=strlen(a) ; for(int i=0;i<m;++i){ if(a[i]==' '
)cout<<' '; else { memset(b,0,sizeof(b)); int k=0; while(a[i]!=' '&&i<m){
b[k]=a[i]; k++; i++; } i--; reverse(b,b+k);cout<<b; } } }

友情链接
KaDraw流程图
API参考文档
OK工具箱
云服务器优惠
阿里云优惠券
腾讯云优惠券
华为云优惠券
站点信息
问题反馈
邮箱:ixiaoyang8@qq.com
QQ群:637538335
关注微信