C++ program to print the given string in reverse order

Program to reverse a string

Today in this article we will learn how to reverse a string. There are various method top reverse a sting that we will be discussing in this page.

Reversing a string is a technique so that the 1st character becomes the last character and so on.

Lets understand this with the help of an example:- 

  • Input sting:- RITIKA
  • Output sting:- AKITIR
reverse a string

Algorithm

  • Step 1. Take the string which you have to reverse as the input variable says str.
  • Step 2:- Calculate the length of the string. The actual length of the string is one less than the number of characters in the string. Let actual length be len.
  • Step 3:- Use a for loop to iterate the string using a temporary variable to swap the elements.
  • Step 4:- Print the reversed string.
C++ Program to Reverse A String

C++ Code :-

Method 1

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

using namespace std;

// function definition of the revstr()

void revstr(char *str1)
{
// declare variable
int i, len, temp;
len = strlen(str1);
// use strlen() to get the length of str string
// use for loop to iterate the string
for (i = 0; i < len/2; i++) {
// temp variable use to temporary hold the string
temp = str1[i];
str1[i] = str1[len - i - 1];
str1[len - i - 1] = temp;
}
}
int main()
{
char str[50];
// size of char string
cout<<"Enter the string: ";
gets(str); // use gets() function to take string
cout<<" Before reversing the string: \n"<<str;
// call revstr() function
revstr(str);
cout<<" After reversing the string:"<<str;
return 0;
}

Output:-

Before reversing the string: priyanka 
After reversing the string: aknayirp
Before reversing the string: ritika
After reversing the string: akitir

Method 2

We can also reverse the string using some build in functions:-

  • like strrev() in C++ programming language
  • strrev() is a built in function in C++ which comes under string.h header file
  • this function returns the string after reversing it.
#include<iostream.h>
#include<string.h>


using namespace std;

int main()
{
// declare the size of character string
char str[100];
  cout<<"Enter a string to be reversed: ";
cin>>str;

// use strrev() function to reverse a string
cout<<"After the reverse of a string: "<<strrev(str);
return 0;
}

Output:-

Enter a string to be reversed: Ritika
After the reverse of a string: akitiR
Enter a string to be reversed: priyanka
After the reverse of a string: aknayira

Method 3

We can also reverse the string by storing the reverse in another array and then print.

  • The idea is to transfer the string to another string in a reverse manner let the another array is rev.
  • All we need is the size of the string.
  • The method is to initialize a character array of the equal size and start copying the elements of the input string from the end.
  • And display the rev
#include<stdio.h>
#include<string.h>
using namespace std;

int main()
{
char str[1000], rev[1000];
int i, j, count = 0;
cout<<"Enter a string to be reversed: ";
gets(str);
cout<<"\nString Before Reverse: "<<str;
//finding the length of the string
while (str[count] != '\0') {
count++;
}
j = count - 1;
//Storing the array to another array
for (i = 0; i <= count; i++) {
rev[i] = str[j];
j--;
}
cout<<"\nString After Reverse: "<< rev;
return 0;
}

Output

Enter a string to be reversed: priya

String Before Reverse: priya
String After Reverse: ayirp

Enter a string to be reversed: prakriti

String Before Reverse: prakriti
String After Reverse: itirkarp

Method 4

  • This includes use of two pointers one at the starting and one at the end.
  • the characters are then reversed one by one
  • The process of reversing the string is done with the help of two pointers.
#include<string.h>
#include<iostream.h>
using namespace std;

// function declaration str_len that 
int str_len(char *ptr) 
{ 
  int i = 0; 
  while ( *(ptr + i) != '\0') 
   i++; 
  return i; 
}

void revstr(char *st) 
{ 
  int len, i; 
  char *start, *end, temp; 
  len = str_len (st); 
  start = st; 
  end = st;

  for (i = 0; i < len - 1; i++) 
  end++;

  for (i = 0; i < len/2; i++) 
 { 
  temp = *end; 
  *end = *start; 
  *start = temp;
  start++; 
  end--; 
 } 
}


int main() 
{ 
  char str[100]; 
  // Take the string that need to be reversed as a input from the user
  cout<<"Enter a string to be reversed: "; cin>>str; 
  cout<<"String Before Reverse: "<< str <<"\n"; 
  revstr(str);
  cout<<"String After Reverse: "<< str ; 
  return 0; 
}

Output

Enter a string to be reversed: priya
String Before Reverse: priya
String After Reverse: ayirp
Enter a string to be reversed: Aditya
String Before Reverse: Aditya
String After Reverse: aytidA