Vowels and Consonants ISC Computer Science 2015 Practical

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 the 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 headings 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.

WordVowelsConsonants
Intelligence57
Plus13
Character36
Is11
Education54

Example 2
INPUT: God is great.
OUTPUT:
God Is Great.

WordVowelsConsonants
God12
Is11
Great23

Example 3
INPUT: All the best!
OUTPUT:
Invalid Input.

import java.util.Scanner;
import java.util.StringTokenizer;
class Find{
    public static void main(String[] args){
        Scanner in = new Scanner(System.in);
        System.out.print("Enter the sentence: ");
        String s = in.nextLine();
        char last = s.charAt(s.length() - 1);
        if(last != '.' && last != '?'){
            System.out.println("Invalid Input.");
            return;
        }
        String t = "";
        for(int i = 0; i < s.length(); i++){
            char ch = s.charAt(i);
            if(i == 0 || s.charAt(i - 1) == ' ')
                t += Character.toUpperCase(ch);
            else
                t += Character.toLowerCase(ch);
        }
        System.out.println(t);
        StringTokenizer st = new StringTokenizer(t, " .?");
        System.out.println("Words\tVowels\tConsonants");
        int count = st.countTokens();
        for(int i = 1; i <= count; i++){
            String w = st.nextToken();
            int c = 0;
            int v = 0;
            for(int j = 0; j < w.length(); j++){
                char ch = w.charAt(j);
                if(Character.isLetter(ch)){
                    if("AEIOUaeiou".indexOf(ch) >= 0)
                        v++;
                    else
                        c++;
                }
            }
            System.out.println(w + "\t" + v + "\t" + c);
        }
    }
}

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