Latest Tutorials| Questions and Answers|Ask Questions?|Site Map



Home Java Beginners String Reverse in Java

 
 

Share on Google+Share on Google+

String Reverse in Java

Advertisement
In this section, we are going to reverse a given string.

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.

Advertisements

If you enjoyed this post then why not add us on Google+? Add us to your Circles



Liked it!  Share this Tutorial


Follow us on Twitter, or add us on Facebook or Google Plus to keep you updated with the recent trends of Java and other open source platforms.

Posted on: July 6, 2007

Related Tutorials

Discuss: String Reverse in Java   View All Comments

Post your Comment


Your Name (*) :
Your Email :
Subject (*):
Your Comment (*):
  Reload Image
 
 
Comments:26
ramesh
April 23, 2012
java

import java.io.*; class str { public static void main(String a[])throws IOException { DataInputStream dt=new DataInputStream(System.in); String s; System.out.println("enter the string"); s=dt.readLine(); System.out.println("value="+s); String s1=new StringBuffer(s).reverse().toString(); System.out.println("value="+s1); } }
kanagavelrajb
April 28, 2012
program

frequently asks interview program questions with solution using C and Java
shan
June 2, 2012
reverse a string

thank
Chandria
July 20, 2012
computer 21L1

the execution of the crting is not correct..
kannu
August 7, 2012
about program

ye programs run to hote hi nae h.................... age se sath me run kaise kare ye tarika b bta dena.............. nahi to ye kisi kam ke nahi h .......sayad kuch alg method ho inka.......................................................
apurva
August 14, 2012
string

no comment
abhishek
September 28, 2012
Coments

Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: 0 at StringReverseExample.main(StringReverseExample.java:5)
ahmed
October 4, 2012
java

this program isnt useful at all its all bullshit and i think you guys should change the way you guys explain it
Danie
April 4, 2014
stuff

keep up the good work admin
DMCA.com