Home Java Beginners String Reverse in Java
Questions:Ask|Latest


 
 

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.

Advertisement

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

Ask Questions?    Discuss: String Reverse in Java   View All Comments

Post your Comment


Your Name (*) :
Your Email :
Subject (*):
Your Comment (*):
  Reload Image
 
 
Comments
kamal
April 18, 2011
java.lang.ArrayIndexOutOfBoundsException: 0

it came out error Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: 0 at StringReverseExample.main(StringReverseExample.java:15)
Ankit Srivastava
April 18, 2011
String pattern

when i give the input like My name is Ankit then output will be Ankit is name my please help me how can i get this...
Dell
April 23, 2011
Rus

Thank you!
S1
June 13, 2011
array index out of bounds

it threw array index out of bounds exception.
glene
June 27, 2011
object oriented programming

How to reverse a string then compare to the original string
saravanan
September 5, 2011
string compare in java

super
mcrhine
September 10, 2011
is it works?

is it works?
Indu kirthiga
September 10, 2011
Question on java

Which data type can be used to store 10 lakh digit in a variable in java??????And also value should be retrieved digit by digit...
james
September 14, 2011
C++

how about in C++ code?
Swapna
September 15, 2011
hiiiii

the above program is wrong
syira
October 10, 2011
question

let say if i wanted just the word how to be reverse.how can i do it??can you show some examples??thanks
Jackson
October 16, 2011
Thank you

Thanks i was looking for this link.
Dni
November 1, 2011
hi,

i am getting Exception ArrayBoundException..plz give me the reverseString prog by giving input through the keyboard...
Shivangi Nigam
November 21, 2011
Error in the above code

String reverse = new StringBuffer(string). Semi colon missing in the above line you cant make the reference of a string to string buffer error message there. correct code: StringBuffer reverse = new StringBuffer(string); reverse().toString(); reverse is not a function or constructor its a variable correct code: reverse.toString();
Umair
January 31, 2012
String

import java.util.Scanner; public class sortString { public static void main(String args[]){ Scanner input=new Scanner(System.in); String s; String reverse=""; System.out.println("Enter String:"); s=input.next(); for(int i=0;i<s.length();i++){ reverse=s.charAt(i)+reverse; } System.out.println("Reverse String= "+reverse); } }
prashnat
March 5, 2012
....

please tell me the basic logic not using inbuilt method
shiva
April 18, 2012
JVM architure in java

GOOD PROCESS
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
DMCA.com