Python,C,C++ and JAVA programs for CBSE, ISC, B.Tech and I.T Computer Science and MCA students

The Programming Project: ISC COMPUTER SCIENCE PRACTICALS 2008 ~ Vowels & Words Count

Saturday, August 2, 2014

ISC COMPUTER SCIENCE PRACTICALS 2008 ~ Vowels & Words Count



ISC COMPUTER SCIENCE PRACTICALS 2008 ~ Vowels & Words Count in Strings
A sentence is terminated by either “ . ” , “ ! ” or “ ? ” followed by a space.
Input a piece of text consisting of sentences. Assume that there will be a
maximum of 10 sentences in a block.
Write a program to:
(i)  Obtain the length of the sentence (measured in words) and the frequency
     of vowels in each sentence.
(ii) Generate the output as shown below using the given data

Sample data:

INPUT
HELLO! HOW ARE YOU? HOPE EVERYTHING IS FINE. BEST OF LUCK.
OUTPUT

Sentence    No. of Vowels    No. of words
----------------------------------------------------------
1        2        1
2        5        3 
3        8        4
4        3        3       

import java.util.*;
import java.io.*;
public class StringVowelcount {
    public static void main(String[] args) throws IOException
        {
        Sorting fc = new Sorting();
        String msg;
        BufferedReader br = new BufferedReader(new InputStreamReader(System.in));   
        System.out.println(" Enter the sentence:");
        msg = br.readLine();
        if ( msg.charAt(msg.length()-1) != '.' && msg.charAt(msg.length()-1) != '?' && msg.charAt(msg.length()-1) != '!' ) {
            System.out.println("Paragraph terminated incorrectly, appending '.' at the end");
            msg += '.';   
            }
        Vowel v = new Vowel(msg);
       
        v.senCount();
        v.sentenceExtraction();
        System.out.println("Sentence    No. of Vowels    No. of words");
        System.out.println("--------------------------------------------------------");
        v.vowelWordCount();
        System.out.println("");
        }
    }
class Vowel {
    Vowel(String msg) {
    paragraph  = msg;
    wordCount  = new int[paragraph.length()-1];
    vowelCount = new int[paragraph.length()-1];
    sentenceCount = 0;
    }
    public void senCount() {
        for ( int i = 0; i < paragraph.length(); i++) {
            if( paragraph.charAt(i) == '.' || paragraph.charAt(i) == '?' || paragraph.charAt(i) == '!' )
                sentenceCount++;
            }
        sentences = new String[sentenceCount];
        }
    public void sentenceExtraction() {
        int j = 0;
        System.out.println("The sentences are:");
        for ( int i = 0; i < paragraph.length(); i++) {
            sentences[j] = "";
            while(paragraph.charAt(i) != '.' && paragraph.charAt(i) != '?' && paragraph.charAt(i) != '!') {
                sentences[j] += paragraph.charAt(i);
                i++;
                if ( i == paragraph.length())
                    break;
                }
            sentences[j] += paragraph.charAt(i);   
            System.out.println(sentences[j]);
            j++;
            i++;
            }
         }   
    public void vowelWordCount() {
        String tmp;
        for (int i = 0; i < sentenceCount; i++) {
            wordCount[i] = 1;
            vowelCount[i] = 0;
            tmp = sentences[i].toUpperCase();
            for (int j =0; j < sentences[i].length(); j++) {
                if ( tmp.charAt(j) == ' ')
                    wordCount[i]++;
                if ( tmp.charAt(j) == 'A' || tmp.charAt(j) == 'E' || tmp.charAt(j) == 'I' ||tmp.charAt(j) == 'O' ||tmp.charAt(j) == 'U')   
                    vowelCount[i]++;
                }
            System.out.println(i+1+"        "+vowelCount[i]+"        "+wordCount[i]);   
            }
        System.out.println("");   
        }   
    private int sentenceCount; 
    private int[] wordCount;
    private int[] vowelCount;
    private String paragraph;
    private String[] sentences;   
    }

No comments:

Post a Comment