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

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

    I think the easiest way to reverse a string would be by using the inbuilt reverse function.

    Including the header file #include you can:

    #include  
    #include 
    
    int main() 
    { 
        std::string str = "Programming"; 
      
       
        std::reverse(str.begin(), str.end()); 
      
        std::cout << str; 
        return 0; 
    } 
    

    Output:

    gnimmargorP
    

提交回复
热议问题