Friday, August 27, 2010

BlueJ Program On Rearranging The Words Of A Text In Reverse Order



Here is a string program on which changes the position of the words but not the alphabets. Details of the program are given below.
  
The input here will consists of a number of lines of English text consisting of the letters of the English alphabet, the punctuation marks (‘) apostrophe, (.) full stop, (,) comma, (;) semicolon, (:) colon and white space characters (blank, new line). Your task is to print the words of the text in reverse order without a punctuation mark other than blanks. For example consider the following input text:
‘This is a sample piece of text to illustrate this problem. If you are smart you will solve this right’.
 The corresponding output would read as:
‘right this solve will you smart are you If problem this illustrate to text of piece sample a is This’.
That is, the lines are printed in reverse order. Note: Individual words are not reversed. The first line of input contains a single integer N( < = 20), indicating the number of lines in the input. This is followed by N lines of input text. Each line should accept a maximum of 80 characters.

import java.io.*;
import java.util.*;
class ReverseSentence
{
 String str;
 BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
 StringTokenizer stk;
 String arr[]=new String[60];
 int index=0;
 String s1;
 public void take() throws IOException
 {
   System.out.println("Enter the sentence:");
  str=br.readLine();
  str=str.substring(0,str.length()-1);
   stk=new  StringTokenizer(str);
    while(stk.hasMoreTokens())
   {
   s1=stk.nextToken();
   if((s1.charAt(s1.length()-1) >=65 && s1.charAt(s1.length()-1)<=90)||(s1.charAt(s1.length()-1) >=97 && s1.charAt(s1.length()-1)<=122))
     arr[index++]=s1;
    else
     arr[index++]=s1.substring(0,s1.length()-1);
    }
    str=" ";
    for(int i=index-1;i >=0;i--)
    {
     str=str+ arr[i]+" ";
    }
    str=str.trim();
    str=str+".";
  }
  public void display()
  {
  System.out.println("Output="+str);
 }
 public static void main(String args[]) throws IOException
 {
  ReverseSentence ss=new ReverseSentence();
  ss.take();
  ss.display();
 }
}

 Variable description of the program

String str – Initial text is stored in this string objecty.
 BufferedReader br – Used to take input from user.
StringTokenizer stk – break the input text into tokens.
 String arr[] – The words of the text are stored here.
 int index= - index of the string array.

Brief study of the program

This program is little bit simple one. The input text is broken into words using StringTokenizer class. The punctuations of the text are eliminated from the words and ultimately they are stored in a string array.
The values are accessed from the end location of the array and are concatenated in another string object and displayed as a text with the words reversed.

4 comments:

  1. Hello Sir,
    In every program,when it comes to the main function,you pass String args[] through the parameters.What is the significance of it.The program runs without it also...

    ReplyDelete
    Replies
    1. I think you run your programs on BlueJ editor only although in Bluej editor also you can call main () function ( while calling the constructor from the Choice control, main function is just below it). In many books ( school level), main function is written as 'public void main ()', but the actual main function is 'public static void main (String args[])'. String args [] is the array of String objects which takes command line arguments (at least one argument is passed always and that is the file name. In all other java editors, excepting BlueJ, 'public static void main (String args[])' is needed. BlueJ is a school level editor, not a professional one.

      Delete

Subscribe via email

Enter your email address:

Delivered by FeedBurner