Thursday, December 9, 2010

BlueJ Program On Reversing A Text – Words Of The Sentences Are Displayed In Reversed Order

The input in this problem will consist 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. The task is to print the words of the text in reverse order without any punctuation marks other than blanks.

Example:

Input: This is a simple piece of text to illustrate this problem
Output: problem this illustrate to text of piece simple a is This

Input format: The first line of input contains a single integer 'n' indicating the number of lines in the input.

This is followed by 'n' lines of input text.

Output format: Output text contains the input lines in reverse order without punctuations except blank spaces
as illustrated above.

Sample input: Number of lines: 2
Enter the text:
Emotions, controlled and directed to work, is character.
By Swamy Vivekananda

Sample output: Vivekananda Swamy By character is work to dedicated and controlled Emotions

import java.io.*;
class RevWords
{
String word=" ",str,str1=" ",substr;
int pos,n;
BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
public void takeString() throws Exception
{
System.out.println("Number of lines:");
n=Integer.parseInt(br.readLine().trim());
System.out.println("Enter the sentence:");
 for(int i=0;i< n;i++)
  {
   str1=" ";
    str=br.readLine().trim();
   while(true)
 {

  pos=str.lastIndexOf(' ');
  if(pos==-1)
  break;
  substr=str.substring(pos).trim();
  str=str.substring(0,pos).trim();
  if(substr.toUpperCase().charAt(substr.length()-1)>=65 && substr.toUpperCase().charAt(substr.length()-1)<=90)
  str1=str1+ " " + substr;
  else
 str1=str1 + " " + substr.substring(0,substr.length()-1) ;
}
  if(str.toUpperCase().charAt(str.length()-1)>=65 && str.toUpperCase().charAt(str.length()-1)<=90)
  str1=str1 + " "+str;
  else
 str1=str1+ " "+ str.substring(0,str.length()-1);
 str1=str1.trim();
 word=str1+ " " + word;
}
word=word.trim();
System.out.println(word);
}
public static void main(String args[]) throws Exception
{
 RevWords object=new RevWords();
 object.takeString();
 }
}

Variable description of the program

String word : It stores the reversed text.
String str : This String object stores each lines of text
String str1 : This string object is used to store each line in reversed order
String substr : It is used to retrieve each word from the string ‘str’
int pos : It holds the location of space in the text
int n : Used to hold number of lines text input.
BufferedReader br : BufferedReader class object for terminal input.

How to proceed in the program

‘public void takeString()’ function takes the number of lines to be entered and the value is stored in variable ‘n’. Using the for loop within the function body, each line of text is stored in ‘str’. Using the while loop the sentence stored in ‘str’ is converted into words starting from the end using ‘lastIndexOf(' ')’ function of String class and the words are concatenated in the string object ‘str1’. When this process completes, the reversed string is concatenated at the front of the string object ‘word’. And this above steps are carried out until the for loop terminates. At the end the string object ‘word’ is printed.

The same program of reversing a text word-wise using BlueJ can be done using a different technique.


Related Post:  BlueJ Programs on String/Sentence

1 comment:

Subscribe via email

Enter your email address:

Delivered by FeedBurner