String Reverse in Java

In this example we are going to reverse a given string.
This example takes values from command line
argument as string, buffers the input string by using the StringBuffer(String string)
method, reverse the buffered string and converts this buffered string into
the string by using the toString() method. Note that pass the words
within " " to wrap the words into a single string value at
the command line argument.
The code of the program is given below:
public class StringReverseExample
{
public static void main(String[] args)
{
String string=args[0];
String reverse = new StringBuffer(string).
reverse().toString();
System.out.println("\nString before reverse:
"+string);
System.out.println("String after reverse:
"+reverse);
}
}
|
The output of the program is given below:
C:\rajesh\kodejava>javac StringReverseExample.java
C:\rajesh\kodejava>java StringReverseExample "hi
how are you."
String before reverse: hi how are you.
String after reverse: .uoy era woh ih
|
Download this example.

|
Current Comments
2 comments so far (post your own) View All Comments Latest 10 Comments:Can you please show me some advanced applet programming ? I would be very greateful if you could sir.
thanking you,
yours faithfully,
Pema
Posted by Pema on Friday, 05.16.08 @ 14:47pm | #60148
Regarding your String Reverse example, how to reverse the order of each word of the String after reverse its order from the original. So that the result will be: "you are how hi".
To continue on your example what are the commands will be used??
Posted by Peg on Wednesday, 04.23.08 @ 04:39am | #57636