编程论坛
注册
登录
编程论坛
→
C++教室
C+ + 编程问题
烟雨霏微
发布于 2018-11-07 21:07, 1671 次点击
从键盘将一个字符串输入到字符数组中,按反序存放。例如输入“Abcd e”,则输出“e dcbA”。
3 回复
#2
复旦
2018-11-08 01:23
#include <cstdio>
#include <cstring>
#include <iostream>
#include <algorithm>
using namespace std;
int main()
{
string s;
scanf ("%s",s);
reverse(s.begin(),s.end()); //全场MVP
字符串反转
printf ("%s",s);
return 0;
}
注意头文件。
#3
rjsp
2018-11-08 08:52
程序代码:
#include
<iostream>
#include
<string>
#include
<algorithm>
#include
<iterator>
using
namespace
std;
int
main(
void
)
{
//
先输入,这没什么好说的
string
s;
getline(
cin
, s );
//
然后可以先反序,再输出
//
std::reverse( s.begin(), s.end() );
//
cout << s << endl;
//
或者不反序,而是反着输出
copy( s.rbegin(), s.rend(), std::ostream_iterator<
char
>(
cout
) );
}
#4
陈紫文
2018-11-08 16:51
可以使用反向迭代器,从尾向头进行输出
1