#GPT-3.5 | Conversation #1113923973965697074
1 messages · Page 1 of 1 (latest)
how can i invert a string in c++?
To invert a string in C++, you can use the built-in reverse() function from the <algorithm> library. Here's an example:
#include <iostream>
#include <algorithm>
using namespace std;
int main() {
string str = "hello world";
reverse(str.begin(), str.end()); // using reverse function
cout << str << endl; // output: "dlrow olleh"
return 0;
}
In this example, we define a string variable str with the value "hello world". We then call the reverse() function with the begin() and end() iterators of the string object str. This will reverse the order of the characters in the string. Finally, we output the result using the cout stream.
close this conversation
Alright! If you have any other questions in the future, feel free to message me again. Have a good day!
This ChatGPT conversation has restarted. Reply in this thread to start the conversation.