How do you reverse a string in place in C or C++?

前端 未结 30 1886
长发绾君心
长发绾君心 2020-11-22 00:37

How do you reverse a string in C or C++ without requiring a separate buffer to hold the reversed string?

相关标签:
30条回答
  • 2020-11-22 01:34

    You use std::reverse algorithm from the C++ Standard Library.

    0 讨论(0)
  • 2020-11-22 01:35

    Reverse a string in place (visualization):

    Reverse a string in place

    0 讨论(0)
  • 2020-11-22 01:35

    Another C++ way (though I would probably use std::reverse() myself :) as being more expressive and faster)

    str = std::string(str.rbegin(), str.rend());
    

    The C way (more or less :) ) and please, be careful about XOR trick for swapping, compilers sometimes cannot optimize that.

    In such case it is usually much slower.

    char* reverse(char* s)
    {
        char* beg = s, *end = s, tmp;
        while (*end) end++;
        while (end-- > beg)
        { 
            tmp  = *beg; 
            *beg++ = *end;  
            *end =  tmp;
        }
        return s;
    } // fixed: check history for details, as those are interesting ones
    
    0 讨论(0)
  • My answer would be similar to most of them, but please find my code here.

    //Method signature to reverse string
    string reverseString(string str);
    
    int main(void){
        string str;
        getline(cin, str);
        str =  reverseString(str);
        cout << "The reveresed string is : " << str;
        return 0;
    }
    
    /// <summary>
    ///     Reverses the input string.
    /// </summary>
    /// <param name="str">
    ///    This is the input string which needs to be reversed.
    /// </param>
    /// <return datatype = string>
    ///     This method would return the reversed string
    /// </return datatype>
    
    string reverseString(string str){
        int length = str.size()-1;
        char temp;
        for( int i=0 ;i<(length/2);i++)
        {
            temp = str[i];
            str[i] = str[length-i];
            str[length-i] = temp;
        }
        return str;
    }
    
    0 讨论(0)
  • 2020-11-22 01:38

    It's been a while and I don't remember which book taught me this algorithm, but I thought it was quite ingenious and simple to understand:

    char input[] = "moc.wolfrevokcats";
    
    int length = strlen(input);
    int last_pos = length-1;
    for(int i = 0; i < length/2; i++)
    {
        char tmp = input[i];
        input[i] = input[last_pos - i];
        input[last_pos - i] = tmp;
    }
    
    printf("%s\n", input);
    
    0 讨论(0)
  • 2020-11-22 01:38
    #include <cstdio>
    #include <cstdlib>
    #include <string>
    
    void strrev(char *str)
    {
            if( str == NULL )
                    return;
    
            char *end_ptr = &str[strlen(str) - 1];
            char temp;
            while( end_ptr > str )
            {
                    temp = *str;
                    *str++ = *end_ptr;
                    *end_ptr-- = temp;
            }
    }
    
    int main(int argc, char *argv[])
    {
            char buffer[32];
    
            strcpy(buffer, "testing");
            strrev(buffer);
            printf("%s\n", buffer);
    
            strcpy(buffer, "a");
            strrev(buffer);
            printf("%s\n", buffer);
    
            strcpy(buffer, "abc");
            strrev(buffer);
            printf("%s\n", buffer);
    
            strcpy(buffer, "");
            strrev(buffer);
            printf("%s\n", buffer);
    
            strrev(NULL);
    
            return 0;
    }
    

    This code produces this output:

    gnitset
    a
    cba
    
    0 讨论(0)
提交回复
热议问题