- Data Structure
- Networking
- RDBMS
- Operating System
- Java
- MS Excel
- iOS
- HTML
- CSS
- Android
- Python
- C Programming
- C++
- C#
- MongoDB
- MySQL
- Javascript
- PHP
- Physics
- Chemistry
- Biology
- Mathematics
- English
- Economics
- Psychology
- Social Studies
- Fashion Studies
- Legal Studies
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
How to quickly reverse a string in C++?
In this section, we will see how to reverse a string very quickly using C++. For reversing there is a built-in function in the algorithm library, called reverse(). This function takes the beginning and the ending pointer of a container, then reverse the elements.
Input: A number string “Hello World” Output: “dlroW olleH”
Algorithm
Step 1:Take a string Step 2: reverse it using reverse() function Step 3: Print the result. Step 4: End
Example Code
#include<iostream> #include<algorithm> using namespace std; main() { string my_str = "This is a string to be reversed"; cout << "Initial state of this string: " << my_str << endl; //reverse using the reverse() function for reversing a string reverse(my_str.begin(), my_str.end()); cout << "Final state of this string: " << my_str; }
Output
Initial state of this string: This is a string to be reversed Final state of this string: desrever eb ot gnirts a si sihT
Advertisements
To Continue Learning Please Login
Login with Google