Java
class Reverse
{
public static void main()
{
String orig, rev = "";
Scanner in = new Scanner(System.in);
System.out.println("Enter a string to reverse");
orig = in.nextLine();
int length = orig.length();
for (int i = length - 1 ; i >= 0 ; i--)
rev = rev + orig.charAt(i);
System.out.println("Reverse of the string: " + rev);
}
}
{
public static void main()
{
StringBuffer a = new StringBuffer("Sarthak Mund S3");
System.out.println("Reversed Steing is : "+a.reverse());
}
}
Reverse a String in JAVA
02 January
0
Write a program in java to accepts line of strings and display the strings in reverse order.
import java.util.*;class Reverse
{
public static void main()
{
String orig, rev = "";
Scanner in = new Scanner(System.in);
System.out.println("Enter a string to reverse");
orig = in.nextLine();
int length = orig.length();
for (int i = length - 1 ; i >= 0 ; i--)
rev = rev + orig.charAt(i);
System.out.println("Reverse of the string: " + rev);
}
}
Output |
"OR"
class RevStr{
public static void main()
{
StringBuffer a = new StringBuffer("Sarthak Mund S3");
System.out.println("Reversed Steing is : "+a.reverse());
}
}
OutPut |
Must Read :-
Previous article
Next article
Leave Comments
Post a Comment