翻转字符串

来源:互联网 发布:濮院淘宝供货 编辑:程序博客网 时间:2024/05/16 00:55

1.使用string.h中的strrev函数

#include <iostream>#include <cstring>using namespace std;int main(){       // c语言中表示单个字符的时候使用单引号,当表示字符串的时候使用双引号    char s[] = "hello";    strrev(s);    cout << s << endl;    return 0;}

2.使用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;}

3.手写算法

#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;}
0 0
原创粉丝点击