Skip to content
Related Articles

Related Articles

Improve Article

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

  • Difficulty Level : Easy
  • Last Updated : 19 May, 2021
Geek Week
 

Given a string, write a C/C++ program to reverse it.
 

string-reverse

 

  1. Write own reverse function by swapping characters: One simple solution is to write our own reverse function to reverse a string in C++
     

CPP




// A Simple C++ program to reverse a string
#include <bits/stdc++.h>
using namespace std;
 
// Function to reverse a string
void 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 program
int main()
{
    string str = "geeksforgeeks";
    reverseStr(str);
    cout << str;
    return 0;
}
  1. Output :
skeegrofskeeg
  1.  
  2. 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);
  1.  

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;
}
  1. Output :
skeegrofskeeg
  1.  
  2. Only printing reverse
     

CPP




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

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 that
char* 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 code
int main(void)
{
    const char *s = "GeeksforGeeks";
    printf("%s", reverseConstString(s));
    return (0);
}
  1. Output: 
     
skeeGrofskeeG
  1. Reverse string using the constructor : Passing reverse iterators to the constructor returns us a reversed string.
     

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;
}
  1. Output: 
     
skeeGrofskeeG

     5. Using a temporary string

C++




// 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

 

?list=PLqM7alHXFySGg6GSRmE2INI4k8fPH5qVB
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
 

Attention reader! All those who say programming isn’t for kids, just haven’t met the right mentors yet. Join the  Demo Class for First Step to Coding Coursespecifically designed for students of class 8 to 12. 

The students will get to learn more about the world of programming in these free classes which will definitely help them in making a wise career choice in the future.




My Personal Notes arrow_drop_up
Recommended Articles
Page :