C++ 字符逆序:

#include <iostream>

#include <string>

int main()

{

char s[101];

cin>>s;

reverse(s);

cout<<s<<endl;

}

c++标准库有对容器进行逆序的操作,字符串本身就是字符的数组。可以直接使用C++标准类库函数reverse。

#include <iostream>

#include <string>

#include <algorithm>

using namespace std;int main()

{ string str; //C++输入流完全支持string类型,没必要定义C的字符buffer。

cin>>str; //输入字符串

reverse(str.begin(), str.end()); //str执行完这句,就已经是逆序结果。

cout<<str<<endl; return 0;

}

Logo

权威|前沿|技术|干货|国内首个API全生命周期开发者社区

更多推荐