Wednesday, March 18, 2009

WORD REVERSE

/*

Exercise 1
Program Name: WORD REVERSE
Programmer name: Danver G. Palmiano
Date Started: March 18,2009, 8:30am
Date Ended: March 18, 2009,11:24am
Aim: To enter a word that will reverse from the first index to last. if two words entered, then the place of the word won't change but its index will reverse.

*/

import javax.swing.*;
import java.util.Scanner;
import java.io.*;


public class Reverse {

public static void main(String[] args) throws IOException//start the main method that will catch Exceptions
{

String a=JOptionPane.showInputDialog("Enter Name: ");//ask input to the user in correct form
System.out.print("Input:"+a);//print output of the user

String b=a.substring(a.indexOf(" "),a.length());
String reverse= new StringBuffer(b).reverse().toString();//reverse the first word entered
String c=a.substring(0,a.indexOf(" "));
String reverse2=new StringBuffer(c).reverse().toString();//reverse the second word entered


System.out.println("\nInput in Normal Form : " + a);// Print the normal string

System.out.println("Reverse input: " +reverse2+ " "+ reverse );// Print the string in reversed order
}//end main method


}//end class Reverse


Sample Output:


Enter name: dan ver

Input in Normal Form: dan ver
Reverse input: nad rev



source:SAVITCH, WALTER"Java: AN INTRODUCTION TO COMPUTER SCIENCE AND PROGRAMMING",THIRD EDITION

No comments:

Post a Comment