Reverse a String

Reverse a String” is a basic operation which can be applied over string. Here, we are given a string of length ‘n’ and our task is to write a program to reverse the given string without using in-built function. 

Example:

INPUT:
ABCDEFGH
OUTPUT:
HGFEDCBA

There are multiple ways to reverse a string. Some of them are:

METHOD 1: Using Temporary String:

Copy the string into temporary string in reverse order and copy back the temporary string to original string.

METHOD 2: Swap Elements:

Swap first element of string with last element of string, second element of string with second last element and so on. 


METHOD 1: Reverse a String using Temporary String

In this method, create a temporary string of same length as original string and copy all the elements of the original string to temporary string in reverse order. Finally, copy back temporary string to original string in original order.

Time Complexity: O(n), where n is length of string


C++ Program to reverse a string using temporary string is as follows:

/* Program to reverse a string */
#include<bits/stdc++.h> 
#define len 1000 
using namespace std;  
int main()  
{  
    char str[len];
    cout<<"Enter a string:\n";
    cin>>str;  
    cout<<"Before Reversing String:\n";  
    cout<<str;  
    int length = strlen(str);  
    char temp[length+1];  
    int pos = 0;  
    /* Copy the original string to  
        temporary string in reverse order */  
    for(int i = length - 1; i >= 0; i--)  
    {  
        temp[pos] = str[i];  
        pos++;  
    }  
    temp[pos] = '
 /* Program to reverse a string */ #include<bits/stdc++.h> #define len 1000 using namespace std; int main() { char str[len]; cout<<"Enter a string:\n"; cin>>str; cout<<"Before Reversing String:\n"; cout<<str; int length = strlen(str); char temp[length+1]; int pos = 0; /* Copy the original string to temporary string in reverse order */ for(int i = length - 1; i >= 0; i--) { temp[pos] = str[i]; pos++; } temp[pos] = '\0'; /* Copy back the temporary string to original string in original order */ for(int i = 0; i < length; i++) { str[i] = temp[i]; } cout<<"\nAfter Reversing String:\n"; cout<<str; } 
'; /* Copy back the temporary string to original string in original order */ for(int i = 0; i < length; i++) { str[i] = temp[i]; } cout<<"\nAfter Reversing String:\n"; cout<<str; }
OUTPUT:
Enter a String: 
sdfdfv
Before Reversing String:
sdfdfv
After Reversing String:
vfdfds

METHOD 2: Swap Elements

In this method, auxiliary string is not used. Here, we swap the first element of the string with last element of the string, second element of string with second last element and so on, until string is completely reversed.

Time Complexity: O(n), where n is length of string.

C++ Program to reverse a string is as follows:

/* Program to reverse a string */
#include<bits/stdc++.h>  
#define len 1000
using namespace std;  
int main()
int main()  
{  
    char str[len];  
    cout<<"Enter a String:\n";
    cin>>str;  
    cout<<"Before Reversing String:\n";  
    cout<<str;  
    int length = strlen(str);  
    /* Swaping Elements */  
    for(int i = 0, j = length - 1; j >= i; i++,j--)  
    {  
        char temp = str[i];  
        str[i] = str[j];  
        str[j] = temp;  
    }  
    cout<<"\nAfter Reversing String:\n";  
    cout<<str;  
      
}  

OUTPUT:
Enter a String: 
sdfdfv
Before Reversing String:
sdfdfv
After Reversing String:
vfdfds

Related Posts:

  1. Program to Count Number of Characters in a String.
  2. Program to Count frequency of particular character of the String.
  3. Program to Count Number of Vowels and Number of Consonants in a String.
  4. Program to Check if String is alphabetic or numeric or alpha-numeric.
  5. Program to copy one String to Another.
  6. Program to Concatenate Two Strings.
  7. Program to Check whether given String is Palindrome or not.
  8. Program to convert lower-case alphabet to upper-case alphabet and vice-versa.
  9. Program to count total number of words in a string.
  10. Program to find smallest and largest word of the String.
  11. Program to find Most Frequent Character of the String.
  12. Program to Remove all the blank spaces from the String.
  13. Program to check if String is isogram or not.
  14. Program to Reverse Each word of the String.
  15. Program to Print All the Substring of the Given String.
  16. Program to find longest palindromic Substring.
  17. Program to check Anagram Strings.
  18. Program to check whether two Strings are Rotation of each other or not.
  19. Program to check Palindromic Anagram.
  20. Program to print all the Palindromic Substring of the String.

You may also like...