OneCompiler

java program to reverse a string given by user.

167

import java.util.;
import java.io.
;
public class HelloWorld {
public static void main(String[] args) {
Scanner sc=new Scanner(System.in);
String str=sc.next();
System.out.println("Before reverse :"+str);
//This is a first method to reverse a string.
StringBuffer sb=new StringBuffer(str);
sb.reverse();
System.out.println("After reverse : "+sb);
//This is second method to reverse a string.
char ch[]=str.toCharArray();
System.out.print("After reverse :");
for(int i=ch.length-1;i>=0;i--)
{
System.out.print(ch[i]);
}
System.out.println();
}
}