用string方法把字符串逆顺序输出,如输入light,输出thgil
常见的字符串逆序输出问题,通常有以下两种解决方法:
(1)用字符数组方法;
(2)用string方法。
看题主要求为string方法,则以该种方法来写,代码如下:
#include <iostream>
using namespace std;
int main()
{
string a;
int i, n;
char temp;
cout << "请输入一个字符串:";
cin >> a;
n = int(a.size());
for (i = 0; i < n / 2; i++)
{
temp = a[i];
a[i] = a[n - i - 1];
a[n - i - 1] = temp;
}
cout << a << endl;
return 0;
}
测试结果如图:
希望对题主有所帮助,可以的话,帮忙点个采纳!
#include <iostream>
#include <string>
#include <algorithm>
using namespace std;
int main(){
string s;
cin>>s;
reverse(s.begin(),s.end());
cout<<s;
return 0;
}
有帮助记得采纳哟