Different methods to reverse a string in C/C++ - GeeksforGeeks

03 Feb, 2022
Given a string, write a C/C++ program to reverse it.
Write own reverse function by swapping characters: One simple solution is to write our own reverse function to reverse a string in C++.CPPCPP// A Simple C++ program to reverse a string#include <bits/stdc++.h>using namespace std;  // Function to reverse a stringvoid reverseStr(string& str){    int n = str.length();      // Swap character starting from two    // corners    for (int i = 0; i < n / 2; i++)        swap(str[i], str[n - i - 1]);}  // Driver programint main(){    string str = "geeksforgeeks";    reverseStr(str);    cout << str;    return 0;}Output :skeegrofskeegUsing inbuilt “reverse” function: There is a direct function in “algorithm” header file for doing reverse that saves our time when programming.// Reverses elements in [begin, end]
void reverse (BidirectionalIterator begin, 
BidirectionalIterator end);
CPPCPP// A quickly written program for reversing a string// using reverse()#include <bits/stdc++.h>using namespace std;int main(){    string str = "geeksforgeeks";      // Reverse str[begin..end]    reverse(str.begin(), str.end());      cout << str;    return 0;}Output :skeegrofskeegOnly printing reverse:CPPCPP// C++ program to print reverse of a string#include <bits/stdc++.h>using namespace std;  // Function to reverse a stringvoid reverse(string str){   for (int i=str.length()-1; i>=0; i--)      cout << str[i]; }  // Driver codeint main(void){    string s = "GeeksforGeeks";    reverse(s);    return (0);}Output:skeegrofskeeG
Getting reverse of a const string:CPPCPP// C++ program to get reverse of a const string#include <bits/stdc++.h>using namespace std;  // Function to reverse string and return// reverse string pointer of thatchar* reverseConstString(char const* str){    // find length of string    int n = strlen(str);      // create a dynamic pointer char array    char *rev = new char[n+1];      // copy of string to ptr array    strcpy(rev, str);      // Swap character starting from two    // corners    for (int i=0, j=n-1; i<j; i++,j--)        swap(rev[i], rev[j]);                 // return pointer of the reversed string    return rev;}  // Driver codeint main(void){    const char *s = "GeeksforGeeks";    printf("%s", reverseConstString(s));    return (0);}Output:skeeGrofskeeG
Reverse string using the constructor : Passing reverse iterators to the constructor returns us a reversed string.CPPCPP// A simple C++ program to reverse string using constructor #include <bits/stdc++.h> using namespace std; int main(){      string str = "GeeksforGeeks";      //Use of reverse iterators    string rev = string(str.rbegin(),str.rend());      cout<<rev<<endl;     return 0;}Output:skeeGrofskeeG
Using a temporary string:CPPCPP// A simple C++ program to reverse string using constructor#include <bits/stdc++.h>using namespace std;int main(){      string str = "GeeksforGeeks";    int n=str.length();    //Temporary string to store the reverse    string rev;    for(int i=n-1;i>=0;i--)    rev.push_back(str[i]);      cout<<rev<<endl;    return 0;}Output:skeeGrofskeeG

Write own reverse function by swapping characters: One simple solution is to write our own reverse function to reverse a string in C++.CPPCPP// A Simple C++ program to reverse a string#include <bits/stdc++.h>using namespace std;  // Function to reverse a stringvoid reverseStr(string& str){    int n = str.length();      // Swap character starting from two    // corners    for (int i = 0; i < n / 2; i++)        swap(str[i], str[n - i - 1]);}  // Driver programint main(){    string str = "geeksforgeeks";    reverseStr(str);    cout << str;    return 0;}Output :skeegrofskeeg
CPP
// A Simple C++ program to reverse a string#include <bits/stdc++.h>using namespace std;  // Function to reverse a stringvoid reverseStr(string& str){    int n = str.length();      // Swap character starting from two    // corners    for (int i = 0; i < n / 2; i++)        swap(str[i], str[n - i - 1]);}  // Driver programint main(){    string str = "geeksforgeeks";    reverseStr(str);    cout << str;    return 0;}
skeegrofskeeg
Using inbuilt “reverse” function: There is a direct function in “algorithm” header file for doing reverse that saves our time when programming.// Reverses elements in [begin, end]
void reverse (BidirectionalIterator begin, 
BidirectionalIterator end);
CPPCPP// A quickly written program for reversing a string// using reverse()#include <bits/stdc++.h>using namespace std;int main(){    string str = "geeksforgeeks";      // Reverse str[begin..end]    reverse(str.begin(), str.end());      cout << str;    return 0;}Output :skeegrofskeeg
// Reverses elements in [begin, end]
void reverse (BidirectionalIterator begin, 
BidirectionalIterator end);

CPP
// A quickly written program for reversing a string// using reverse()#include <bits/stdc++.h>using namespace std;int main(){    string str = "geeksforgeeks";      // Reverse str[begin..end]    reverse(str.begin(), str.end());      cout << str;    return 0;}
skeegrofskeeg
Only printing reverse:CPPCPP// C++ program to print reverse of a string#include <bits/stdc++.h>using namespace std;  // Function to reverse a stringvoid reverse(string str){   for (int i=str.length()-1; i>=0; i--)      cout << str[i]; }  // Driver codeint main(void){    string s = "GeeksforGeeks";    reverse(s);    return (0);}Output:skeegrofskeeG

CPP
// C++ program to print reverse of a string#include <bits/stdc++.h>using namespace std;  // Function to reverse a stringvoid reverse(string str){   for (int i=str.length()-1; i>=0; i--)      cout << str[i]; }  // Driver codeint main(void){    string s = "GeeksforGeeks";    reverse(s);    return (0);}
skeegrofskeeG

Getting reverse of a const string:CPPCPP// C++ program to get reverse of a const string#include <bits/stdc++.h>using namespace std;  // Function to reverse string and return// reverse string pointer of thatchar* reverseConstString(char const* str){    // find length of string    int n = strlen(str);      // create a dynamic pointer char array    char *rev = new char[n+1];      // copy of string to ptr array    strcpy(rev, str);      // Swap character starting from two    // corners    for (int i=0, j=n-1; i<j; i++,j--)        swap(rev[i], rev[j]);                 // return pointer of the reversed string    return rev;}  // Driver codeint main(void){    const char *s = "GeeksforGeeks";    printf("%s", reverseConstString(s));    return (0);}Output:skeeGrofskeeG

CPP
// C++ program to get reverse of a const string#include <bits/stdc++.h>using namespace std;  // Function to reverse string and return// reverse string pointer of thatchar* reverseConstString(char const* str){    // find length of string    int n = strlen(str);      // create a dynamic pointer char array    char *rev = new char[n+1];      // copy of string to ptr array    strcpy(rev, str);      // Swap character starting from two    // corners    for (int i=0, j=n-1; i<j; i++,j--)        swap(rev[i], rev[j]);                 // return pointer of the reversed string    return rev;}  // Driver codeint main(void){    const char *s = "GeeksforGeeks";    printf("%s", reverseConstString(s));    return (0);}
skeeGrofskeeG

Reverse string using the constructor : Passing reverse iterators to the constructor returns us a reversed string.CPPCPP// A simple C++ program to reverse string using constructor #include <bits/stdc++.h> using namespace std; int main(){      string str = "GeeksforGeeks";      //Use of reverse iterators    string rev = string(str.rbegin(),str.rend());      cout<<rev<<endl;     return 0;}Output:skeeGrofskeeG

CPP
// A simple C++ program to reverse string using constructor #include <bits/stdc++.h> using namespace std; int main(){      string str = "GeeksforGeeks";      //Use of reverse iterators    string rev = string(str.rbegin(),str.rend());      cout<<rev<<endl;     return 0;}
skeeGrofskeeG

Using a temporary string:CPPCPP// A simple C++ program to reverse string using constructor#include <bits/stdc++.h>using namespace std;int main(){      string str = "GeeksforGeeks";    int n=str.length();    //Temporary string to store the reverse    string rev;    for(int i=n-1;i>=0;i--)    rev.push_back(str[i]);      cout<<rev<<endl;    return 0;}Output:skeeGrofskeeG

CPP
// A simple C++ program to reverse string using constructor#include <bits/stdc++.h>using namespace std;int main(){      string str = "GeeksforGeeks";    int n=str.length();    //Temporary string to store the reverse    string rev;    for(int i=n-1;i>=0;i--)    rev.push_back(str[i]);      cout<<rev<<endl;    return 0;}
skeeGrofskeeG

YouTubeGeeksforGeeks500K subscribersReverse a string in C/C++ | GeeksforGeeksWatch laterShareCopy link27/45InfoShoppingTap to unmuteIf playback doesn't begin shortly, try restarting your device.You're signed outVideos you watch may be added to the TV's watch history and influence TV recommendations. To avoid this, cancel and sign in to YouTube on your computer.CancelConfirmMore videosMore videosSwitch cameraShareInclude playlistAn error occurred while retrieving sharing information. Please try again later.Watch on0:000:000:00 / 1:51•Live•<div class="player-unavailable"><h1 class="message">An error occurred.</h1><div class="submessage"><a href="https://www.youtube.com/watch?v=L44_gKvPZ34" target="_blank">Try watching this video on www.youtube.com</a>, or enable JavaScript if it is disabled in your browser.</div></div>
This article is contributed by Priyam kakati, Ranju Kumari, Somesh Awasthi and improved by Supratik Mitra, Lakshay Bansal. If you like GeeksforGeeks and would like to contribute, you can also write an article and mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please write comments if you find anything incorrect, or you want to share more information about the topic discussed above
Omkar Goulay
supratik_mitra
Lakshay313
shettykaran21
CoderSaty
cpp-string
Reverse
STL
C Language
C++
School Programming
Strings
Strings
STL
Reverse
CPP
Writing code in comment?
Please use ide.geeksforgeeks.org,
generate link and share the link here.
Comments
Old Comments
Command line arguments in C/C++
rand() and srand() in C/C++
Core Dump (Segmentation fault) in C/C++
fork() in C
Exception Handling in C++
Inheritance in C++
Iterators in C++ STL
Initialize a vector in C++ (6 different ways)
Socket Programming in C/C++
Operator Overloading in C++