您的位置:首页 > 其它

字符串反转方法总结

2017-07-27 15:22 246 查看
第一种:使用string.h中的strrev函数

[cpp] view
plain copy

#include <iostream>  

#include <cstring>  

using namespace std;  

  

int main()  

{  

    char s[]="hello";  

  

    strrev(s);  

  

    cout<<s<<endl;  

  

    return 0;  

}  

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

[cpp] view
plain copy

#include <iostream>  

#include <string>  

#include <algorithm>  

using namespace std;  

  

int main()  

{  

    string s = "hello";  

  

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

  

    cout<<s<<endl;  

  

    return 0;  

}  

第三种:自己编写

[cpp] view
plain copy

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

}  
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: