Reverse String C++ using char array

前端 未结 5 621
暖寄归人
暖寄归人 2020-12-03 01:52

I wrote a simple C++ program to reverse a string. I store a string in character array. To reverse a string I am using same character array and temp variable to swap the char

5条回答
  •  [愿得一人]
    2020-12-03 02:28

    If I were you, I would just write it like so:

    int main()
    {
        string str;
        cout << "Enter a string: " << endl;
        getline(cin, str);
        for (int x = str.length() - 1; x > -1; x--)
        {
            cout << str[x];
        }
        return 0;
    }
    

    This is a very simple way to do it and works great.

提交回复
热议问题