Q:

Program to find reverse of a string

belongs to collection: String Programs

0

Explanation

In this program, we need to find the reverse of the string. This can be done by iterating the string backward and storing each character from the original string into a new string.

Original string: Dream big  

Reverse of the string: big maerD  

Algorithm

  1. Define a string and another empty string to store the reversed string.
  2. Now, iterate the string from last and store a character from back into variable revString.
  3. At the end of the loop, revString will hold the reverse of the string.

Input:

string = "Dream big"  

Output:

Original string: Dream big
Reverse of given string: gib maerD

All Answers

need an explanation for this answer? contact us directly to get an explanation for this answer

Python

string = "Dream big";  
#Stores the reverse of given string  
reversedStr = "";  
   
#Iterate through the string from last and add each character to variable reversedStr  
for i in range(len(string)-1, -1, -1):  
    reversedStr = reversedStr + string[i];  
      
print("Original string: " + string);  
#Displays the reverse of given string  
print("Reverse of given string: " + reversedStr);  

 

Output:

Original string: Dream big
Reverse of given string: gib maerD

 

C

#include <stdio.h>  
#include <string.h>  
   
int main()  
{  
    char string[] = "Dream big";  
    //Stores the reverse of given string  
    char reversedStr[strlen(string)];  
    int j = 0;  
      
    //Iterate through the string from last and add each character to variable reversedStr  
    for(int i = strlen(string)-1; i >= 0; i--){  
        reversedStr[j++] = string[i];  
    }  
    reversedStr[j] = '\0';  
      
    printf("Original string: %s", string);  
    //Displays the reverse of given string  
    printf("\nReverse of given string: %s", reversedStr);  
   
    return 0;  
}  

 

Output:

Original string: Dream big
Reverse of given string: gib maerD

 

JAVA

public class Reverse  
{  
    public static void main(String[] args) {  
        String string = "Dream big";  
        //Stores the reverse of given string  
        String reversedStr = "";  
          
        //Iterate through the string from last and add each character to variable reversedStr  
        for(int i = string.length()-1; i >= 0; i--){  
            reversedStr = reversedStr + string.charAt(i);  
        }  
          
        System.out.println("Original string: " + string);  
        //Displays the reverse of given string  
        System.out.println("Reverse of given string: " + reversedStr);  
    }  
}  

 

Output:

Original string: Dream big
Reverse of given string: gib maerD

 

C#

using System;  
                      
public class Reverse  
{  
    public static void Main()  
    {  
        String string1 = "Dream big";  
        //Stores the reverse of given string  
        String reversedStr = "";  
          
        //Iterate through the string from last and add each character to variable reversedStr  
        for(int i = string1.Length-1; i >= 0; i--){  
            reversedStr = reversedStr + string1[i];  
        }  
          
        Console.WriteLine("Original string: " + string1);  
        //Displays the reverse of given string  
        Console.WriteLine("Reverse of given string: " + reversedStr);  
    }  
}  

 

Output:

Original string: Dream big
Reverse of given string: gib maerD

 

PHP

<!DOCTYPE html>  
<html>  
<body>  
<?php  
$string = "Dream big";  
//Stores the reverse of given string  
$reversedStr = "";  
   
//Iterate through the string from last and add each character to variable reversedStr  
for($i = strlen($string)-1; $i >= 0; $i--){  
    $reversedStr = $reversedStr . $string[$i];  
}  
   
print("Original string: " . $string);  
//Displays the reverse of given string  
print("<br>Reverse of given string: " . $reversedStr);  
?>  
</body>  
</html> 

 

 

Output:

Original string: Dream big
Reverse of given string: gib maerD

need an explanation for this answer? contact us directly to get an explanation for this answer

total answers (1)

This question belongs to these collections

Similar questions


Program to find the duplicate characters in a stri... >>
<< Program to find maximum and minimum occurring char...