c++中字符串的反轉


1.對於用char定義的字符串:使用string.h中的strrev函數

#include <iostream>
#include <cstring>
using namespace std;
int main()
{
    char s[]="123456";//不能是string類型;
    strrev(s);
    cout<<s<<endl;
    return 0;
}

2.對於string類型的:使用algorithm中的reverse函數

#include<iostream>
#include <cstring>
#include <algorithm>
using namespace std;
int main()
{
    string s[]="123456";
    reverse(s.begin(),s.end());
    cout<<s<<endl;
    return 0;
}

3.自己編寫函數:對於字符串的兩邊進行交換。

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM