Java Program to find Reverse of the string.

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.

  1. Original string: Dream big  
  2. Reverse of the string: big maerD  

ALGORITHM

  • STEP 1: START
  • STEP 2: DEFINE String string = “Dream big”
  • STEP 3: DEFINE reversedStr = ” “
  • STEP 4: SET i =string.length()-1. REPEAT STEP 5 to STEP 6 UNTIL i>=0
  • STEP 5: reversedStr = reversedStr + string.charAt(i)
  • STEP 6: i = i – 1
  • STEP 7: PRINT string.
  • STEP 8: PRINT reversedStr.
  • STEP 9: END

Program:

  1.   public class Reverse   
  2. {    
  3.     public static void main(String[] args) {    
  4.         String string = “Dream big”;    
  5.         //Stores the reverse of given string    
  6.         String reversedStr = “”;    
  7.             
  8.         //Iterate through the string from last and add each character to variable reversedStr    
  9.         for(int i = string.length()-1; i >= 0; i–){    
  10.             reversedStr = reversedStr + string.charAt(i);    
  11.         }    
  12.             
  13.         System.out.println(“Original string: ” + string);    
  14.         //Displays the reverse of given string    
  15.         System.out.println(“Reverse of given string: ” + reversedStr);    
  16.     }    
  17. }    

Output:

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

Leave a Comment