Friday, August 27, 2010

BlueJ Program On String Array - Arranging The Words In Ascending Order



Today’s program is on string manipulation. This type of program is very much essential for ISC students. In practical examination of ISC Computer Science every year, programs on string manipulation using string array object is a common feature.

 Here is the program on string array

Accept a paragraph of text consisting of sentences that are terminated
 by either “.”, “,”, “!” or a “?” followed by a space. Assume that there can be a maximum of 05 sentences in a paragraph.
Design a program to perform the following :
(a) Arrange the sentences in alphabetical order of words, sentence by sentence.
(b) Separate the words which begin with a vowel.

Sample data 1:
INPUT: HELLO ! HOW ARE YOU ? WHEN ARE YOU COMING ? HOPE TO SEE YOU SOON.
OUTPUT: HELLO ! ARE HOW YOU ? ARE COMING WHEN YOU ? HOPE SEE SOON TO YOU.
VOWELS: ARE
Sample data 2 :
INPUT : THE QUICK BROWN FOX JUMPED OVER THE LAZY DOG.
OUTPUT : BROWN DOG FOX JUMPED LAZY OVER QUICK THE THE.
VOWELS: OVER

Here are the codes of the program




import java.io.*;
import java.util.*;
class String1
{
 String str,str3;
 BufferedReader br;
 StringTokenizer stk;
 String str1[];
 String str2[];
 String vowel[];
 int i,j,k,len,x=0,y=0,z=0;
 String1()
 {
 str1=new String [10];
 str2=new String [10];
 vowel=new String [200];
 br=new BufferedReader(new InputStreamReader(System.in));
 }
  public void take()throws IOException
  {
   System.out.println("Enter the paragraph of sentences:");
   str=br.readLine();
   str=str.substring(0,str.length()-1);
   stk=new StringTokenizer(str,"!?",true);
    while(stk.hasMoreTokens())
    {
      str1[x++]=stk.nextToken();
    }
    for(i=0;i< x;i++)
   {
    str=str1[i];
     stk=new StringTokenizer(str," ");
    y=0;
    while(stk.hasMoreTokens())
    {
      str2[y++]=stk.nextToken();
    }
   
    for(j=0;j< y-1;j++)
    {
     for(k=j+1;k< y;k++)
     {
      if(str2[j].compareTo(str2[k]) >0)
      {
       str3=str2[j];
       str2[j]=str2[k];
       str2[k]=str3;
      }
      }
      }
      for(j=0;j< y;j++)
      {
      vowel[z++]=str2[j];
      System.out.print(" "+str2[j]);
      }
     
      y=0;
      }
      System.out.println(".");
      System.out.println("\nVowels:");
      for(j=0;j< z;j++)
      {
     str=vowel[j].trim().toUpperCase();
    if(str.charAt(0)=='A' ||str.charAt(0)=='E' ||str.charAt(0)=='I' ||str.charAt(0)=='O' ||str.charAt(0)=='U')
      System.out.print(vowel[j]+" ");
      }
      }
      public static void main(String args[])throws IOException
      {
       String1 ob=new String1();
       ob.take();
       }
      }
  Brief study of the program

The paragraph is entered in a string object firstly. StringTokenizer class is used in this program to break the text into tokens in respect of punctuations as it is given in the program that paragraph is to be broken into sentences in respect of punctuation and stored in a string array object ‘str1’. This step ensures that all sentences are stored in the string array. Next step is to break the sentences again in words and stored in another string array ‘str2’. These words are then sorted in ascending order and displayed. Another very interesting job is performed here, all the words of ‘str2’ are stored in the third string array ‘vowel’ from which words starting with vowels will be searched. This step is required as values of the string array ‘str2’ will be changed every time a new sentence from the string array ‘str1’ is broken into words.

14 comments:

  1. give me the link for isc practical 2007 Question 2 and isc practical 2008 Question 2

    ReplyDelete
  2. Can you give me the program of 2D boundary elements sorting? Its an important program for ISC 2011. please can you kindly send me the link here: suyatrasinha@gmail.com

    ReplyDelete
  3. Your program is posted on today. Please check the page.

    ReplyDelete
  4. Be specific. Please send me the codes you have tested with your entered values and output.

    ReplyDelete
  5. can u tell d differences and similarities btwn while and for loop

    ReplyDelete
  6. sir please post the above program without string tokenizer please..........

    ReplyDelete
  7. stk=new StringTokenizer(str,"!?",true);
    while(stk.hasMoreTokens())
    {
    str1[x++]=stk.nextToken();
    }
    for(i=0;i< x;i++)
    {
    str=str1[i];
    stk=new StringTokenizer(str," ");
    y=0;
    while(stk.hasMoreTokens())
    {
    str2[y++]=stk.nextToken();
    }
    SIR PLZ EXPLAIN THESE LINES FROM TOP WHY U HAVE TAKEN
    stk=new StringTokenizer(str,"!?",true);
    AND
    WHY NOT
    stk=new StringTokenizer(str);ONLY ALSO BELOW
    stk=new StringTokenizer(str," ");

    ReplyDelete
    Replies
    1. The paragraph is terminated using the punctuations and the sentences are to be sorted, that's why the paragraph is broken into sentences using 'stk=new StringTokenizer(str,"!?",true);'. In case of 'stk=new StringTokenizer(str," ");', you can use stk=new StringTokenizer(str);

      Please go through StringTokenizer class for more details.

      Delete
  8. sir i need help on array of objects plz

    ReplyDelete

Subscribe via email

Enter your email address:

Delivered by FeedBurner