Program to reverse a String in java

program to reverse a String without use of function in java

Reversestring.java

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
package logic;
 
import java.util.Scanner;
 
public final class Reversestring 
{
	public static void main(String[] args)
	{
 
	     String input;
	     String reverse="";
 
	    Scanner scn=new Scanner(System.in);
	    System.out.println("enter a string");
 
            //Taking input from user
	    input=scn.nextLine();
 
            int length=input.length();
	    for(int i=length-1;i>=0;i--)
	    {
	    	reverse=reverse +input.charAt(i);
	    }
 
	    System.out.println("reverse string  "+reverse);
	}
 
}

output:

1
2
3
enter a string
bhavesh
reverse string  hsevahb

Leave a Reply

Your email address will not be published. Required fields are marked *