Common Words ISC Computer Science 2021 Practical

Write a program to accept a paragraph containing two sentences only. The sentences may be terminated by either '.', '?' or '!' only. Any other character may be ignored. The words are to be separated by a single blank space and must be in uppercase.

Perform the following tasks:

(a) Check for the validity of the accepted paragraph for the number of sentences and for the terminating character.

(b) Separate the two sentences from the paragraph and find the common words in the two sentences with their frequency of occurrence in the paragraph.

(c) Display both the sentences separately along with the common words and their frequency, in the format given below.

Test your program for the following data and some random data:

Example 1:
INPUT:
IS IT RAINING? YOU MAY GET WET IF IT IS RAINING.
OUTPUT:
IS IT RAINING?
YOU MAY GET WET IF IT IS RAINING.

COMMON WORDSFREQUENCY
IS2
IT2
RAINING2

Example 2:
INPUT:
INDIA IS MY MOTHERLAND AND I AM PROUD OF MY MOTHERLAND. ALL INDIANS ARE MY BROTHERS AND SISTERS.
OUTPUT:
INDIA IS MY MOTHERLAND AND I AM PROUD OF MY MOTHERLAND.
ALL INDIANS ARE MY BROTHERS AND SISTERS.
COMMON WORDSFREQUENCY
MY3
AND2

Example 3:
INPUT:
ARE YOU COMING? I AM GETTING LATE.
OUTPUT:
ARE YOU COMING?
I AM GETTING LATE.

NO COMMON WORDS

Example 4:
INPUT:
AT LAST, THE TIGER WAS SAVED.
OUTPUT:
INVALID INPUT

import java.util.Scanner;
import java.util.StringTokenizer;
class CommonWords{
    public static void main(String[] args){
        Scanner in = new Scanner(System.in);
        System.out.print("Paragraph: ");
        String p = in.nextLine().toUpperCase();
        p = p.trim();
        if(!isValid(p)){
            System.out.println("INVALID INPUT");
            return;
        }
        int pos = 0;
        for(int i = 0; i < p.length(); i++){
            char ch = p.charAt(i);
            if(".?!".indexOf(ch) >= 0){
                pos = i;
                break;
            }
        }
        String s1 = p.substring(0, pos + 1);
        String s2 = p.substring(pos + 1).trim();
        System.out.println(s1);
        System.out.println(s2);
        StringTokenizer st = new StringTokenizer(s1, " .?!");
        int count = st.countTokens();
        String w1[] = new String[count];
        for(int i = 0; i < count; i++)
            w1[i] = st.nextToken();
        st = new StringTokenizer(s2, " .?!");
        count = st.countTokens();
        String w2[] = new String[count];
        for(int i = 0; i < count; i++)
            w2[i] = st.nextToken();
        String taken[] = new String[w1.length + w2.length];
        int index = 0;
        boolean isCommon = false;
        for(int i = 0; i < w1.length; i++){
            for(int j = 0; j < w2.length; j++){
                if(w1[i].equals(w2[j])){
                    isCommon = true;
                    break;
                }
            }
        }
        if(isCommon)
            System.out.println("COMMON WORDS\tFREQUENCY");
        for(int i = 0; i < w1.length; i++){
            count = 0;
            for(int j = 0; j < w1.length; j++){
                if(w1[i].equals(w1[j]))
                    count++;
            }
            boolean flag = false;
            for(int k = 0; k < w2.length; k++){
                if(w1[i].equals(w2[k])){
                    count++;
                    flag = true;
                    isCommon = true;
                }
            }
            if(count > 1 && flag && !inTaken(taken, w1[i])){
                System.out.println(w1[i] + "\t\t" + count);
                taken[index++] = w1[i];
            }
        }
        if(!isCommon)
            System.out.println("NO COMMON WORDS");
    }
    public static boolean isValid(String p){
        int len = p.length();
        char last = p.charAt(len - 1);
        if(".?!".indexOf(last) == -1)
            return false;
        int count = 0;
        for(int i = 0; i < len; i++){
            char ch = p.charAt(i);
            if(".?!".indexOf(ch) >= 0)
                count++;
        }
        if(count != 2)
            return false;
        return true;
    }
    public static boolean inTaken(String t[], String w){
        boolean status = false;
        for(int i = 0; i < t.length; i++){
            if(w.equals(t[i])){
                status = true;
                break;
            }
        }
        return status;
    }
}

Comments

Popular posts from this blog

Encrypt Program ISC Specimen 2023 Theory

No Repeat Program ISC Computer Science 2022 Semester 2 Theory

Bank Inheritance Program ISC Specimen 2023 Theory