Free CoreJava Programs Source codes | Examples | Video Tutorials Download

Wednesday, January 21, 2009

Reverse String Using Command Line Arguments

import java.io.BufferedReader;

import java.io.InputStreamReader;

/**

* Accepts a string from user and reverses it out.

*/

public class ReverseString

{

/**

* gets the string input from user and reverse it and display it

* @throws Exception

*/

public static void getStringNreverse() throws Exception

{

// get the input string from user..

BufferedReader brConsole=new BufferedReader(new InputStreamReader(System.in));

System.out.println("Enter Any String (mixture of digits & alphabets) : ");

String strInput=brConsole.readLine();

/// check length of input

if(strInput==null || strInput.length()<=0)

System.out.println(strInput + " Is Invalid Input!!!");

else // reverse string and display

{

System.out.println("Reverse String of " + strInput + " is " + new StringBuffer(strInput).reverse());

}

}

/**

* excecution begin from here

* @param args

*/

public static void main(String[] args)

{

try

{

getStringNreverse();

}

catch(Exception e)

{

System.out.println("Error occured!!");

}

}

}

No comments:

Post a Comment

Followers