package javaPrograms;
import java.util.Scanner;
public class ReverseString
{
//static String reverseString;
public static void main(String a[])
{
Scanner sc=new Scanner(System.in);
System.out.println("enter string to reverse\n\n");
String str=sc.nextLine();
//String str=sc.next(); for single word reverse
String reverseString="";
for (int i=str.length()-1;i>=0; i--)
{
reverseString=reverseString + str.charAt(i);
}
System.out.println("Reverse string is= "+reverseString);
}
}
Output
-------------------------------------------------------------------------------------------------------
enter string to reverse
Learn Java Program for Interview
Reverse string is= weivretnI rof margorP avaJ nraeL
import java.util.Scanner;
public class ReverseString
{
//static String reverseString;
public static void main(String a[])
{
Scanner sc=new Scanner(System.in);
System.out.println("enter string to reverse\n\n");
String str=sc.nextLine();
//String str=sc.next(); for single word reverse
String reverseString="";
for (int i=str.length()-1;i>=0; i--)
{
reverseString=reverseString + str.charAt(i);
}
System.out.println("Reverse string is= "+reverseString);
}
}
Output
-------------------------------------------------------------------------------------------------------
enter string to reverse
Learn Java Program for Interview
Reverse string is= weivretnI rof margorP avaJ nraeL
0 Comments