ISC practical papers › ISC 2010 › Question 3
Frequency Table Of Words
ISC 2010 Computer Science Practical, Question 3 · Strings
The question
Input a paragraph containing ‘n’ number of sentences where (1 = < n < 4). The words are to be separated with a single blank space and are in UPPERCASE. A sentence may be terminated either with a full stop ‘.’ or a question mark ‘?’ only. Any other character may be ignored. Perform the following operations:
(i) Accept the number of sentences. If the number of sentences exceeds the limit, an appropriate error message must be displayed.
(ii) Find the number of words in the whole paragraph
(iii) Display the words in ascending order of their frequency. Words with same frequency may appear in any order.
Example 1
INPUT:
Enter number of sentences: 1
Enter sentences
TO BE OR NOT TO BE.
OUTPUT:
Total number of words: 6
WORD FREQUENCY
OR 1
NOT 1
TO 2
BE 2
Example 2
INPUT:
Enter number of sentences: 3
Enter sentences:
THIS IS A STRING PROGRAM.IS THIS EASY?YES,IT IS.
OUTPUT:
Total number of words: 11
WORD FREQUENCY
A 1
STRING 1
PROGRAM 1
EASY 1
YES 1
IT 1
THIS 2
IS 3
Example 3
INPUT:
Enter number of sentences: 5
OUTPUT:
INVALID ENTRY
Question: ISC 2010 Computer Science Paper 2 (Practical), Question 3, set by the Council for the Indian School Certificate Examinations (CISCE). Solution and video: Vinay Singh.
Java solution
import java.util.*;
class Frequency{
private String word[];
private int frequency[];
private String output;
public Frequency(String paragraph){
output="";
word=paragraph.split("[.?, ]");
frequency = new int[word.length];
prepareFrequencyTable();
}
private void prepareFrequencyTable(){
// Populating the arrays
int limit = word.length;
for( int i=0; i<limit; i++){
frequency[i]=1;
for( int j=i+1; j<limit;j++){
if(word[i].equals(word[j])){
word[j]=word[limit-1];
frequency[i]++;
limit--;
j--;
}
}
}
// Sorting the arrays
int tempFreq;
String tempWord;
boolean swapped;
for( int pass=0; pass<limit-1;pass++){
swapped=false;
for( int position=0; position<limit-1-pass; position++){
if(frequency[position+1]<frequency[position]){
tempFreq=frequency[position+1];
frequency[position+1]=frequency[position];
frequency[position]=tempFreq;
tempWord=word[position+1];
word[position+1]=word[position];
word[position]=tempWord;
swapped=true;
}
}
if(!swapped) break;
}
output="\tWORD\t\tFREQUENCY\n";
for( int i=0;i<limit;i++){
output+="\t"+word[i]+"\t\t"+frequency[i]+"\n";
}
}
public String toString(){
return output;
}
public int getLength(){
return word.length;
}
}
class ISC2010Q3 {
public static void main(String[] args) {
Scanner in = new Scanner(System.in);
System.out.println("INPUT:");
System.out.print("\tEnter number of sentences: ");
int n = in.nextInt();
in.nextLine();
if(n>=1 && n<4){
System.out.print("\tEnter sentences:\n\t");
String paragraph=in.nextLine();
Frequency obj = new Frequency(paragraph);
System.out.println("\tTotal number of words: "+obj.getLength());
System.out.println(obj);
}else{
System.out.println("OUTPUT:\nINVALID ENTRY");
}
in.close();
}
}
All these solutions in one free book
Pragmatic Solutions to ISC Computer Science Practical Problems collects every program from 2004 to 2021 in a 202-page PDF.