Reverse String

How can I reverse the contents of a std::string?

Reversing the contents of a std::string can be done easily using standard library functions. Here are a few methods to achieve this.

Using std::reverse

The std::reverse algorithm from the <algorithm> header is the simplest way to reverse a string. For example:

#include <iostream>
#include <string>
#include <algorithm>

int main(){
  std::string Text{"Hello, World!"};

  std::reverse(Text.begin(), Text.end());

  std::cout << Text;
}
!dlroW ,olleH

Using a Custom Function

You can also write a custom function to reverse a string if you need more control over the process. For example:

#include <iostream>
#include <string>

void ReverseString(std::string& str) {
  size_t n = str.size();
  for (size_t i = 0; i < n / 2; ++i) {
    std::swap(str[i], str[n - i - 1]);  
  }
}

int main() {
  std::string Text{"Hello, World!"};

  ReverseString(Text);

  std::cout << Text;
}
!dlroW ,olleH

Using Stack

You can use a stack to reverse a string, which might be useful in certain contexts, such as parsing algorithms. For example:

#include <iostream>
#include <string>
#include <stack>

int main() {
  std::string Text{"Hello, World!"};
  std::stack<char> Stack;

  for (char c : Text) {
    Stack.push(c);
  }

  std::string Reversed;
  while (!Stack.empty()) {
    Reversed += Stack.top();
    Stack.pop();  
  }

  std::cout << Reversed;
}
!dlroW ,olleH

Using Iterators

If you prefer using iterators, you can construct a reversed string using reverse iterators. For example:

#include <iostream>
#include <string>

int main() {
  std::string Text{"Hello, World!"};

  std::string Reversed(
    Text.rbegin(), Text.rend()
  );  

  std::cout << Reversed;
}
!dlroW ,olleH

Considerations

  • Performance: std::reverse is the most efficient method for most use cases.
  • Readability: Custom functions and iterators can make the code more readable and understandable for specific needs.

By using these techniques, you can easily reverse the contents of a std::string in C++.

Manipulating std::string Objects

A practical guide covering the most useful methods and operators for working with std::string objects and their memory

Questions & Answers

Answers are generated by AI models and may not have been reviewed. Be mindful when running any code on your device.

Concatenate Strings in C++
How can I concatenate two std::string objects in C++?
The append() Method vs += Operator
What is the difference between append() and += operator for strings in C++?
Insert Substring in C++
How can I insert a substring into a specific position within a std::string?
Replace Substring in C++
How can I replace a substring within a std::string with another string?
Iterate over String
How can I iterate through each character of a std::string using a range-based for loop?
Reserve vs Resize
What is the difference between reserve() and resize() methods in std::string?
Convert to wstring
How can I convert a std::string to a std::wstring?
Count Character Occurrences
How can I count the number of occurrences of a character in a std::string?
Restrict String Length
How can I ensure a std::string does not exceed a specific length?
Trim Whitespace
How can I trim whitespace from the beginning and end of a std::string?
Compare String vs Vector
What are the differences between std::string and std::vector?
Or Ask your Own Question
Get an immediate answer to your specific question using our AI assistant