C++题目 字符串逆序输出

2025-02-17 03:36:02
推荐回答(4个)
回答1:

#include

#include

using namespace std;

int main()

{


    string a;

    getline(cin,a);

    string b(a.rbegin(),a.rend());

    cout<


return 0;

}


回答2:

//使用STL实现,四行代码即可,供参考:
#include 
#include 
#include 
using namespace std;
void main()
{
 string strTmp = "";
 cin >> strTmp;
 reverse(strTmp.begin(), strTmp.end());
 cout << strTmp << endl;
}

回答3:

告诉你方法你自己去实现

就是 统计字符串的长度len;
然后第一个字符和最后一个对调
第2个和倒数第2个对调
如果len是奇数那么刚好对调(len-1)/2次
len是偶数就对调len/2次
自己想想就是这么搞 这样算法效率最高 只需要1个临时变量就可以

最快回答这个 其实是一种取巧的做法 没有真正把字符串就地逆置

回答4:

#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;
}