#include
#include
using namespace std;
int main()
{
string a;
getline(cin,a);
string b(a.rbegin(),a.rend());
cout<
return 0;
}
//使用STL实现,四行代码即可,供参考:
#include
#include
#include
using namespace std;
void main()
{
string strTmp = "";
cin >> strTmp;
reverse(strTmp.begin(), strTmp.end());
cout << strTmp << endl;
}
告诉你方法你自己去实现
就是 统计字符串的长度len;
然后第一个字符和最后一个对调
第2个和倒数第2个对调
如果len是奇数那么刚好对调(len-1)/2次
len是偶数就对调len/2次
自己想想就是这么搞 这样算法效率最高 只需要1个临时变量就可以
最快回答这个 其实是一种取巧的做法 没有真正把字符串就地逆置
#include
#include
#define N 100
using namespace std;
int main(int argc,char** argv)
{
char text[N]={0};
cout<<"请输入一字符串:"<cin.getline(text,N);
int len=strlen(text);
char* p=text+len;
cout<<"逆序输出为:"<while ((p--)!=text)
{
cout<<*p;
}
cout<return 0;
}