zoukankan      html  css  js  c++  java
  • C++字符串反转

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

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

      #include <iostream>

      #include <cstring>

      using namespace std;

      

      int main()

      {

        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;

      }

  • 相关阅读:
    Python 简单总结
    Python 简单总结
    Python 简介
    Python基础题
    Python基础题
    tDQSS
    parameter–precharge, tRCD and tRAS
    parameter–key parameters
    parameter -- tWR
    命令集
  • 原文地址:https://www.cnblogs.com/wjq13752525588/p/13512625.html
Copyright © 2011-2022 走看看