C++ Program to Find the Reverse of String or Number (5 different ways)

In this program, we will learn how to find the reverse a number or string in C++ programming language.

We will be discussing a total of 5 ways.

  • Finding reverse of a number
    1. Using While loop
    2. Using recursion
  • Finding reverse of a string
    1. Using inbuilt function
    2. Using iterative method (for loop)
    3. And by using recursion

Let's discuss these ways one by one.

Find the Reverse of a Number

Using while loop

Firstly,  you have to ask the user to enter a number and store it in num variable. Now, start reversing that number to find its reverse and then display its reverse in the output.

To reverse a number, first make a variable say rev and place 0 to rev initially, and make one more variable say rem. Now place the modulus of the number to rem and place rev*10+rem to rev, now divide the number with 10 and continue until number will become 0.

Code:-

#include <iostream>  

using namespace std;  

int main()  
{  
    int num, rev=0, rem;    
    cout<<"Enter a number: ";    
    cin>>num;    
    while(num!=0)    
    {    
        rem = num%10;      
        rev = rev*10+rem;    
        num /= 10;    
    }    
    cout<<"Reversed Number: " << rev << endl;     
    return 0;  
}

Output:-

Enter a number: 5486
Reversed Number: 6845

Using recursion

In this, we will use recursion. The recursive method reverseNumber() is used.

Code:-

#include <iostream>
#include <math.h>
 
using namespace std;
 

int reverseNumber(int num);
 
int main()
{
    int num, reverse;
 
    cout<<"Enter a number: ";
    cin>>num;
 
    reverse = reverseNumber(num);
 
    cout<<"Reverse Number: "<<reverse << endl;
 
    return 0;
}
 
//Recursive function to find reverse of any number 
int reverseNumber(int num)
{
    // Finding total digits in num
    int digits = (int) log10(num);
 
    // Base condition
    if(num == 0)
        return 0;
 
    return ((num%10 * pow(10, digits)) + reverseNumber(num/10));
}

Output:-

Enter a number: 34366
Reverse Number: 66343

Find the Reverse of a String

Using the for loop

In this program the user is asked to enter a string and it is stored in the variable str. The length of str is stored in the variable j and i is initialized as 0. Using a for loop, the string is reversed. The ith character of str is swapped with jth character using a temporary variable temp. The loop terminates when i is less than j. str is then printed which is the reversed string.

Code:-

#include<iostream>
#include<string.h>

using namespace std;

int main ()
{
    char str[50], temp;
    int i, j;
    cout << "Enter a string : ";
    cin >> str;
    j = strlen(str) - 1;
    for (i = 0; i < j; i++,j--){
        temp = str[i];
        str[i] = str[j];
        str[j] = temp;
    }
    cout << "Reversed string : " << str << endl;
    return 0;
}

Output:-

Enter a string : spiderlabweb
Reversed string : bewbalredips

Using the inbuilt function

In this code, we will use the reverse() inbuild function.

Code:-

#include
#include 

using namespace std; 

int main() 
{ 
    string str;

    cout << "Enter a string : "; cin >> str;
    
    // inbuild reverse function 
    reverse(str.begin(), str.end()); 
  
    cout << str << endl; 
    return 0; 
} 

Output:-

Enter a string : spiderlabweb
Reversed string : bewbalredips

Using the recursion

In this, we will use recursion. The recursive method recursiveReverse() is used.

Code:-

#include<iostream>
#include<algorithm> 

using namespace std; 
  
void recursiveReverse(string &str, int i = 0) 
{ 
    int n = str.length(); 
    if (i == n / 2) 
        return; 
    swap(str[i], str[n - i - 1]); 
    recursiveReverse(str, i + 1); 
} 
  

int main() { 
    string str;

    cout << "Enter a string : ";
    cin >> str;

    recursiveReverse(str); 

    cout << "Reversed string : " << str << endl; 
    return 0; 
}

Output:-

Enter a string : spiderlabweb
Reversed string : bewbalredips

You can learn about many other C++ Programs Here.







The following two tabs change content below.

Amit Rawat

Founder and Developer at SpiderLabWeb
I love to work on new projects and also work on my ideas. My main field of interest is Web Development.

You may also like...