首页 > 编程语言 > 详细

c++中字符串反转的3种方法

时间:2018-04-04 17:48:34      阅读:246      评论:0      收藏:0      [点我收藏+]

第一种:使用string.h中的strrev函数

#include <iostream>
#include <cstring>
using namespace std;

int main()
{
    char s[]="hello";

    strrev(s);

    cout<<s<<endl;

    return 0;
}

第二种:使用algorithm中的reverse函数

#include <iostream>
#include <string>
#include <algorithm>
using namespace std;

int main()
{
    string s = "hello";

    reverse(s.begin(),s.end());

    cout<<s<<endl;

    return 0;
}

第三种:自己编写

#include <iostream>
using namespace std;

void Reverse(char *s,int n){
    for(int i=0,j=n-1;i<j;i++,j--){
        char c=s[i];
        s[i]=s[j];
        s[j]=c;
    }
}

int main()
{
    char s[]="hello";

    Reverse(s,5);

    cout<<s<<endl;

    return 0;
}

 

c++中字符串反转的3种方法

原文:https://www.cnblogs.com/mayouyou/p/8718179.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!