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

前端 未结 30 1925
长发绾君心
长发绾君心 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:33

    Note that the beauty of std::reverse is that it works with char * strings and std::wstrings just as well as std::strings

    void strrev(char *str)
    {
        if (str == NULL)
            return;
        std::reverse(str, str + strlen(str));
    }
    

提交回复
热议问题