Q. Write an algorithm and program to reverse a string.

Given a string and the task is to write a algorithm and program to reverse a string. A string is sequence of characters terminated with a null character \0. So we are given a input string and we need to print its reverse order.
For Example: We have given input string as "LETSFINDCOURSE" and it's reverse output we have to find out.
Input="LETSFINDCOURSE"
Output="ESRUOCDNIFSTEL"

Explanation:
1. First declare a string variable and initialize an empty string to "LETSFINDCOURSE".

2. Now we have to calculate the length of the string which we can do using loop(increment the value of count until the string is \0) or using the in-build function(strlen function).

3. Define two variables start = 0 and end = length -1.(Suppose "LETSFINDCOURSE" has a length of 14, then we will swap the 0th character with the 13th character, the 1st character with the 12th character, and the loop will iterate until length / 2 = 7).

4. Iterate the loop from i = 0 to i = length / 2 and increment the variable i by 1.

5. As we already define the start and end variable values, so now we just have to swap the characters one by one and increment the value of start += 1 and decrement the value of end- = 1 during each iteration.

6. After completing iteration print the string and output will be "ESRUOCDNIFSTEL".

Reverse A String Algorithm

//Algorithm To Reverse A String
START
Step 1 ->  Input string str1.
Step 2 ->  calculate the length of the string.
Step 3 ->  define start=0 and end=length-1;
Step 4 ->  for i=0 to length/2.
temp=str1[start];
str1[start]=str1[end];
str1[end]=temp;
start+=1;
end-=1; 
Loop End
step 5->   Display String str1.
STOP


Reverse A String Program

  • C
  • C++
  • Java
  • Python
  • C#
  • PHP
  • //C Program To Reverse A String.
    #include <stdio.h>
    int main()
    {
    char str1[100]="reverse the string";
    int count = 0,i;
    //Count the length using While loop
    while (str1[count] != '\0')
    count++;
    int start=0,end=count-1;
    char temp;
    for(int i=0;i<count/2;i++)
    {   
    //temp is used as a temporary variable that swaps two characters.
    temp=str1[start];
    str1[start]=str1[end];
    str1[end]=temp;
    start+=1;
    end-=1;
    }
    //print the reversed string.
    printf("%s\n", str1);
    return 0;
    }
    
    //C++ Program Reverse A String.
    #include <iostream>
    using namespace std;
    int main()
    {
    string str1="reverse the string";
    //Count the length using in-built function string.length()
    int len1=str1.length(),start=0,end=len1-1;
    char temp;
    for(int i=0;i<len1/2;i++)
    {
    //temp is used as a temporary variable that swaps two characters.
    temp=str1[start];
    str1[start]=str1[end];
    str1[end]=temp;
    start+=1;
    end-=1;
    }
    //print the reversed string.
    cout<<str1;
    return 0;
    }
    
    //Java Program To Reverse A String
    import java.lang.*; 
    import java.io.*; 
    import java.util.*; 
    class Main 
    { 
    public static void main(String[] args) 
    { 
    String str1 = "reverse the string"; 
    char[] str2 = str1.toCharArray(); 
    int start, end=0; 
    //Count the length using in-built function string.length.
    end = str2.length-1; 
    for (start=0; start < end ; start++ ,end--) 
    { 
    //temp is used as a temporary variable that swaps two characters.
    char temp = str2[start]; 
    str2[start] = str2[end]; 
    str2[end]=temp; 
    } 
    //print the reversed string.
    for (char c : str2) 
    System.out.print(c); 
    System.out.println(); 
    } 
    } 
    
    //Python Program To Reverse A String.
    def reverse(str1): 
    str2 = "" 
    for i in str1: 
    \\define a empty string str2 and insert the character from starting.
    str2 = i + str2
    return str2
    str1 = "reverse the string"
    //print the reversed string.
    print (reverse(str1)) 
    
    //C# Program To Reverse A String.
    using System;  
    namespace reverseString  
    {  
    class LFC  
    {  
    static void Main(string[] args)  
    {  
    string str = "reverse the string", reverse = "";  
    int Length = 0;  
    //Count the length using in-built function string.Length.
    Length = str.Length - 1;  
    while(Length>=0)  
    {  
    reverse = reverse + str[Length];  
    Length--;  
    }  
    //print the reversed string.
    Console.WriteLine("{0}",reverse);  
    Console.ReadLine();  
    }  
    }  
    }  
    //PHP Program To Reverse A String.
    <?php
    function reverse($str){ 
    //Count the length using in-built function strlen(string)
    for($i=strlen($str)-1, $j=0; $j<$i; $i--, $j++)  
    {   //temp is used as a temporary variable that swaps two characters.
    $temp = $str[$i]; 
    $str[$i] = $str[$j]; 
    $str[$j] = $temp; 
    } 
    return $str; 
    } 
    $str = "reverse the string"; 
    //print the reversed string.
    print_r(reverse($str)); 
    ?>
    //C Program To Reverse A String.
    #include <stdio.h>
    int main()
    {
    char str1[100]="reverse the string";
    int count = 0,i;
    //Count the length using While loop
    while (str1[count] != '\0')
    count++;
    int start=0,end=count-1;
    char temp;
    for(int i=0;i<count/2;i++)
    {   
    //temp is used as a temporary variable that swaps two characters.
    temp=str1[start];
    str1[start]=str1[end];
    str1[end]=temp;
    start+=1;
    end-=1;
    }
    //print the reversed string.
    printf("%s\n", str1);
    return 0;
    }
    

    Output

    gnists eht esrever
    

    Recommended Programs

       Program to print fibonacci series.
       Program to add two matrices.