TheDeveloperBlog.com

Home | Contact Us

C-Sharp | Java | Python | Swift | GO | WPF | Ruby | Scala | F# | JavaScript | SQL | PHP | Angular | HTML

Program to Find Reverse of a String

Program to Find Reverse of a String on fibonacci, factorial, prime, armstrong, swap, reverse, search, sort, stack, queue, array, linkedlist, tree, graph, pattern, string etc.

<< Back to PROGRAM

Program to find reverse of a string

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.

Solution

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

Next Topic#




Related Links:


Related Links

Adjectives Ado Ai Android Angular Antonyms Apache Articles Asp Autocad Automata Aws Azure Basic Binary Bitcoin Blockchain C Cassandra Change Coa Computer Control Cpp Create Creating C-Sharp Cyber Daa Data Dbms Deletion Devops Difference Discrete Es6 Ethical Examples Features Firebase Flutter Fs Git Go Hbase History Hive Hiveql How Html Idioms Insertion Installing Ios Java Joomla Js Kafka Kali Laravel Logical Machine Matlab Matrix Mongodb Mysql One Opencv Oracle Ordering Os Pandas Php Pig Pl Postgresql Powershell Prepositions Program Python React Ruby Scala Selecting Selenium Sentence Seo Sharepoint Software Spellings Spotting Spring Sql Sqlite Sqoop Svn Swift Synonyms Talend Testng Types Uml Unity Vbnet Verbal Webdriver What Wpf