Thursday, February 27, 2020

Arranging Words From A Sentence According To Length And Alphabetical Order


Write a program to accept a sentence which may be terminated by either '.' , '?' or '!' only. The words are to be separated by a single blank space and are in UPPERCASE.


Perform the following tasks:

(a) Check for the validity of the accepted sentence only for the terminating character.

(b) Arrange the words in ascending order of their length. If two or more Words have the same length, then sort them alphabetically.

(c) Display the original sentence along with the converted sentence.

import java.io.*;
class WordArrange
{
  BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
  void show() throws Exception
 {
      String s,sarr[];
      int i,j,x=0;
      sarr=new String[30];
      System.out.print("\nEnter the sentence: ");
      s=br.readLine();
      i=s.length();
      System.out.println("\nINPUT: "+s);
      if(s.charAt(i-1)!='.' && s.charAt(i-1)!=',' && s.charAt(i-1)!='?' && s.charAt(i-1)!='!')
      {
           System.out.println("\nINVALID INPUT");
           return;
      }
      s=s.substring(0,i-1);
      s=s+ " ";
      while(true)
      {
           i=s.indexOf(' ');
           if(i<=0)
           break;
           sarr[x++]=s.substring(0,i);
           s=s.substring(i+1);
        }
        for(i=0;i<=x-2;i++)
        {
             for(j=i+1;j<=x-1;j++)
             {
                 if(sarr[i].length()>sarr[j].length())
                 {
                     s=sarr[i];
                     sarr[i]=sarr[j];
                     sarr[j]=s;
                 }
                 else if(sarr[i].length()==sarr[j].length() && sarr[i].compareTo(sarr[j])>0)
                 {
                     s=sarr[i];
                     sarr[i]=sarr[j];
                     sarr[j]=s;
                 }
              }
            }
            s="";
            for(i=0;i<=x-1;i++)
            s=s+sarr[i]+" ";
            s=s.trim();                       
            System.out.print("\nOUTPUT: "+s);       
    }
     public static void main(String args[]) throws Exception
     {
         WordArrange ob=new WordArrange();
         ob.show();
      }
    }




Variable Description



Type
Variable
Purpose
Scope
BufferedReader
br
Intake values from user
Used within void show () function
String
s
Stores the sentence entered by user
Used within void show () function
String
sarr[]
Stores the words from the sentence ‘s’
Used within void show () function
Int
x
Index of the String array sarr[]
Used within void show () function
int
i
Loop control variable
Used within void show () function
int
j
Loop control variable
Used within void show () function


Algorithm

Step 1: Create BufferedReader class object br
Step 2: Create int type variables i,j and x with 0 as initial value at x
Step 3: Declare String object ‘s’ and a String array ‘sarr[]’. Create sarr[] with size 10.
Step 4: Take the sentence from user and store in ‘s’.
Step 5: Check whether the sentence ends with the special character.
Step 6: Using while loop break the sentence into words and store in array ‘sarr[]’.
Step 7: Access each word from the array ‘sarr’ and arrange according to the given condition.
Step 8: Concatenate the words from the array ‘sarr’ and display the sentence.

Step 9: End
Alternate Process using StringTokenizer class


import java.util.*;
import java.io.*;
class WordArrange
{
  BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
  StringTokenizer stk;
  void show() throws Exception
 {
      String s,sarr[];
      int i,j,x=0;
      sarr=new String[30];
      System.out.print("\nEnter the sentence: ");
      s=br.readLine();
      i=s.length();
      System.out.println("\nINPUT: "+s);
      if(s.charAt(i-1)!='.' && s.charAt(i-1)!=',' && s.charAt(i-1)!='?' && s.charAt(i-1)!='!')
      {
           System.out.println("\nINVALID INPUT");
           return;
      }
      s=s.substring(0,i-1);
      stk=new StringTokenizer(s);
      while(stk.hasMoreTokens())
      {
           
           sarr[x++]=stk.nextToken();           
        }
        for(i=0;i<=x-2;i++)
        {
             for(j=i+1;j<=x-1;j++)
             {
                 if(sarr[i].length()>sarr[j].length())
                 {
                     s=sarr[i];
                     sarr[i]=sarr[j];
                     sarr[j]=s;
                 }
                 else if(sarr[i].length()==sarr[j].length() && sarr[i].compareTo(sarr[j])>0)
                 {
                     s=sarr[i];
                     sarr[i]=sarr[j];
                     sarr[j]=s;
                 }
              }
            }
            s="";
            for(i=0;i<=x-1;i++)
            s=s+sarr[i]+" ";
            s=s.trim();                       
            System.out.print("\nOUTPUT: "+s);       
    }
     public static void main(String args[]) throws Exception
     {
         WordArrange ob=new WordArrange();
         ob.show();
      }
    }

Variable Description

Type
Variable
Purpose
Scope
BufferedReader
br
Intake values from user
Used within void show () function
String
s
Stores the sentence entered by user
Used within void show () function
String
sarr[]
Stores the words from the sentence ‘s’
Used within void show () function
StringTokenizer
stk
Breaks the sentence into words
Used within void show () function
Int
x
Index of the String array sarr[]
Used within void show () function
int
i
Loop control variable
Used within void show () function
int
j
Loop control variable
Used within void show () function

Algorithm

Step 1: Create BufferedReader class object br
Step 2: Create int type variables i,j and x with 0 as initial value at x
Step 3: Declare String object ‘s’ and a String array ‘sarr[]’. Create sarr[] with size 10.
Step 4: Take the sentence from user and store in ‘s’.
Step 5: Check whether the sentence ends with the special character.
Step 6: Create the StringTokenizer class object stk with 's' as argument.
Step 7: Using while loop access the words from StringTokenizer class object and store in array ‘sarr[]’.
Step 8: Access each word from the array ‘sarr’ and arrange according to the given condition.
Step 9: Concatenate the words from the array ‘sarr’ and display the sentence.

Step 10: End

BACK TO 2020 ISC COMPUTER PRACTICAL PAPER: CLICK HERE

No comments:

Post a Comment

Subscribe via email

Enter your email address:

Delivered by FeedBurner