Alternative method to display the String in reverse form
Accept and display the String in reverse form
import java.util.Scanner; //Calling Scanner Class
public class Reverse
{
public static void main(String []args)
{
System.out.println("Enter String to Reverse it "); //Instruction to user
String str= new Scanner(System.in).nextLine(); //Getting String from User
int i= str.length(); //Calculating Length of Given String
String rev=""; //Declaring and Initializing variable
for(int j=i-1; j>=0; j--) //Looping statement
{
rev= rev+str.charAt(j); //Processing
}
//Printing Result
System.out.println("Reverse of given String is ");
System.out.println(rev);
}
}
This program is used to display the string from the backward. If you haven't visited the other way to display the string in reverse form then, follow the link below:-
Output:
import java.util.Scanner; //Calling Scanner Class
public class Reverse
{
public static void main(String []args)
{
System.out.println("Enter String to Reverse it "); //Instruction to user
String str= new Scanner(System.in).nextLine(); //Getting String from User
int i= str.length(); //Calculating Length of Given String
String rev=""; //Declaring and Initializing variable
for(int j=i-1; j>=0; j--) //Looping statement
{
rev= rev+str.charAt(j); //Processing
}
//Printing Result
System.out.println("Reverse of given String is ");
System.out.println(rev);
}
}
Comments
Post a Comment