Program of Reversing String in C++
Reverse String :
String is defined as the sequence of characters . Reverse string in C++ is printing the character from end to start
How to reverse a String in C++ :
In general, String can be reversed in C++ by using loop or also there is inbuilt method.
Below are the two techniques to reverse the string in C++ :
Method 1 :
#include<iostream> using namespace std; int main() { string str = "atsniperP"; int len = str.length(); cout< <"The reverse string is : "; for(int i=0;i < len;i++){ cout< < str[len-i-1]; } return 0; }
Output :
The reverse string is : Prepinsta
In the above program, we traverse the string using for loop and print the particular character of the string to get the reverse string.
Method 2:
#include<iostream> #include<bits/stdc++.h> using namespace std; int main() { string str = "atsniperP"; reverse(str.begin(), str.end()); cout<<"The reverse string is : "< < str; return 0; }
Output :
The reverse string is : Prepinsta
In the above program, we use the inbuilt method reverse to reverse the string.
Prime Course Trailer
Related Banners
Get PrepInsta Prime & get Access to all 200+ courses offered by PrepInsta in One Subscription
Get over 200+ course One Subscription
Courses like AI/ML, Cloud Computing, Ethical Hacking, C, C++, Java, Python, DSA (All Languages), Competitive Coding (All Languages), TCS, Infosys, Wipro, Amazon, DBMS, SQL and others
Login/Signup to comment