Java program to reverse a string. - BSC IT PRACTICALS

Monday, January 15, 2018

Java program to reverse a string.


CODE:

import java.util.*;
class prac2C
{
String rev="";
public String reversestring(String str)
{
if(str.length()==1)
{
return str;
}
else
{
rev+=str.charAt(str.length()-1)+reversestring(str.substring(0,str.length()-1));
return rev;
}
}
public static void main(String args[])
{
Scanner input=new Scanner(System.in);
prac2C ob=new prac2C();
System.out.print("Enter the String ");
String str=input.nextLine();
System.out.print("Result is "+ob.reversestring(str));
}}

No comments:

Post a Comment