c++ reverse string

#include <iostream>
#include <string>

void reverseString(std::string& str) {
    int start = 0;
    int end = str.length() - 1;

    while (start < end) {
        std::swap(str[start], str[end]);
        start++;
        end--;
    }
}

int main() {
    std::string myString = "Hello, World!";
    std::cout << "Original string: " << myString << std::endl;

    reverseString(myString);

    std::cout << "Reversed string: " << myString << std::endl;

    return 0;
}
  1. Include necessary headers: #include <iostream> and #include <string>.

  2. Define a function reverseString that takes a reference to a std::string as a parameter.

  3. Inside the function, declare two integers, start and end, and initialize them to the beginning and end indices of the string, respectively.

  4. Use a while loop to iterate until the start index is less than the end index.

  5. Inside the loop, use std::swap to swap the characters at the start and end indices.

  6. Increment the start index and decrement the end index after the swap.

  7. In the main function, declare a std::string variable named myString and initialize it with the original string "Hello, World!".

  8. Print the original string using std::cout.

  9. Call the reverseString function, passing myString as an argument, to reverse the string.

  10. Print the reversed string using std::cout.

  11. Return 0 to indicate successful program execution.