Q:

Java Program to find Reverse of the string

belongs to collection: Java String Programs

0

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

  • 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

All Answers

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

Program:

  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

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

total answers (1)

Java String Programs

This question belongs to these collections

Similar questions


need a help?


find thousands of online teachers now
Java program to find the duplicate characters in a... >>
<< Java Program to find maximum and minimum occurring...