Code with Vinay

ISC practical papers › ISC 2015 › Question 3

Title case & vowel/consonant count

ISC 2015 Computer Science Practical, Question 3 · Strings

The question

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. Print an error message if the input does not terminate with ‘.’ Or ‘?’.

You can assume that no word in the sentence exceeds 15 characters, so that you get a proper formatted output.

Perform the following tasks: i) Convert the first letter of each word to uppercase. ii) Find the number of vowels and consonants in each word and display them with proper heading along with the words. Test your program with the following inputs:

Example 1
INPUT:  Intelligence plus character is education.
OUTPUT: Intelligence Plus Character Is Education
Word           Vowels Consonants
Intelligence    5      7
Plus            1      3
Character       3      6
Is              1      1
Education       5      4
Example 2
INPUT:  God is great.
OUTPUT: God Is Great
Word           Vowels Consonants
God             1      2
Is              1      1
Great           2      3
Example 3
INPUT:  All the best!
OUTPUT: INVALID INPUT

Question: ISC 2015 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

ISC2015Q3.java Download GitHub
import java.util.*;

class Sentence {
    private String sentence;
    private static String vowels = "aeiouAEIOU";

    public Sentence(String sentence) {
        this.sentence = sentence;
    }

    public boolean isValid() {
        return (new String(".?")).indexOf(sentence.charAt(sentence.length() - 1)) >= 0;
    }

    public String toString() {
        String output = "";
        String source = sentence.substring(0, sentence.length() - 1);
        String wordList[] = source.split(" ");
        for (String word : wordList) {
            output += Character.toUpperCase(word.charAt(0)) + (word.substring(1)).toLowerCase() + " ";
        }
        return output.trim();
    }

    private static boolean isVowel(char ch) {
        return vowels.indexOf(ch) >= 0;
    }

    private static boolean isConsonant(char ch) {
        return Character.isLetter(ch) && !isVowel(ch);
    }

    public void displayStats() {
        System.out.printf("%-15s %-12s %-12s\n", "Word", "Vowels", "Consonants");
        String source = sentence.substring(0, sentence.length() - 1);
        String wordList[] = source.split(" ");
        String titleCase = "";
        int vowels = 0, consonants = 0;
        char ch;
        for (String word : wordList) {
            titleCase = Character.toUpperCase(word.charAt(0)) + (word.substring(1)).toLowerCase() + " ";
            vowels = 0; consonants = 0;
            for (int position = 0; position < titleCase.length(); position++) {
                ch = titleCase.charAt(position);
                if (isVowel(ch))
                    vowels++;
                else if (isConsonant(ch))
                    consonants++;
            }
            System.out.printf("%-15s %-12d %-12d\n", titleCase, vowels, consonants);
        }
    }
}

class ISC2015Q3 {
    public static void main(String[] args) {
        Scanner in = new Scanner(System.in);
        System.out.print("INPUT:\t");
        String input = in.nextLine();
        System.out.print("OUTPUT:\t");
        Sentence objSentence = new Sentence(input);
        if (objSentence.isValid()) {
            System.out.println(objSentence);
            objSentence.displayStats();
        } else {
            System.out.println("INVALID INPUT");
        }
        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.

Get the free PDF ₹0 ISC 2027 mock papers