Reverse the String Program – Java

Write a Java Program to reverse the letters present in the given String.

- Advertisement -

This Java program reverses letters present in the string entered by a user. For example, Hello People will be termed as elpoeP olleH  Let’s implement the same using Java.

- Advertisement -

import java.util.Scanner;
 
public class ReverseString
{
 public static void main(String[] args)
 {
 System.out.println("Enter string to reverse:");
 
 Scanner read = new Scanner(System.in);
 String str = read.nextLine();
 String reverse = "";
 
 
 for(int i = str.length() - 1; i >= 0; i--)
 {
 reverse = reverse + str.charAt(i);
 }
 
 System.out.println("Reversed string is:");
 System.out.println(reverse);
 }
}

output:

Enter string to reverse:

Hello World

Reversed string is: 

dlroW olleH

 

 

Leave A Reply

Your email address will not be published.

This website uses cookies to improve your experience. We'll assume you're ok with this, but you can opt-out if you wish. AcceptRead More