int to string, pass char

int to string:

  int x=1;

  char c;

  string str;

  c=x+'0'; //The value of c is the ASCII code value of '1'

  str = str + c;

String inversion function:

The first: use the strrev function in string.h
    #include <iostream>  
    #include <cstring>  
    using namespace std;  
      
    intmain()  
    {  
        char s[]="hello";  
      
        strrev (s);  
      
        cout<<s<<endl;  
      
        return 0;  
    }  

The second: use the reverse function in the algorithm
    #include <iostream>  
    #include <string>  
    #include <algorithm>  
    using namespace std;  
      
    intmain()  
    {  
        string s = "hello";  
      
        reverse(s.begin(),s.end());  
      
        cout<<s<<endl;  
      
        return 0;  
    }  

Third: write your own
    #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;  
        }  
    }  
      
    intmain()  
    {  
        char s[]="hello";  
      
        Reverse(s,5);  
      
        cout<<s<<endl;  
      
        return 0;  
    }  




Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325703303&siteId=291194637