Vowel Program ISC 2016 Practical

Write a program to accept a sentence which may be terminated by either ‘.’, ‘?’ or ‘!’ only. The words may be separated by more than one blank space and are in uppercase.

Perform the following tasks:

  1. Find the number of words beginning and ending with a vowel.
  2. Place the words which begin and end with a vowel at the beginning, followed by the remaining words as they occur in the sentence.

Test your program with the sample data and some random data:

Example 1:
INPUT: ANAMIKA AND SUSAN ARE NEVER GOING TO QUARREL ANYMORE.
OUTPUT: NUMBER OF WORDS BEGINNING AND ENDING WITH A VOWEL = 3
ANAMIKA ARE ANYMORE AND SUSAN NEVER GOING TO QUARREL

Example 2:
INPUT: YOU MUST AIM TO BE A BETTER PERSON TOMORROW THAN YOU ARE TODAY.
OUTPUT: NUMBER OF WORDS BEGINNING AND ENDING WITH A VOWEL = 2
A ARE YOU MUST AIM TO BE BETTER PERSON TOMORROW THAN YOU TODAY

Example 3:
INPUT: LOOK BEFORE YOU LEAP.
OUTPUT: NUMBER OF WORDS BEGINNING AND ENDING WITH A VOWEL = 0
LOOK BEFORE YOU LEAP

Example 4:
INPUT: HOW ARE YOU@
OUTPUT: INVALID INPUT

import java.io.*;
class Vowels{
    public static void main(String args[])
    throws IOException{
        InputStreamReader in = new InputStreamReader(System.in);
        BufferedReader br = new BufferedReader(in);
        System.out.print("Sentence: ");
        String s = br.readLine();
        s = s.trim();
        s = s.toUpperCase();
        int len = s.length();
        char last = s.charAt(len - 1);
        if(last != '.' && last != '?' && last != '!'){
            System.out.println("INVALID INPUT");
            return;
        }
        String word = new String();
        String v = new String();
        String nv = new String();
        int count = 0;
        for(int i = 0; i < len; i++){
            char ch = s.charAt(i);
            switch(ch){
                case ' ':
                case '.':
                case '?':
                case '!':
                if(word.length() > 0 && testWord(word)){
                    count++;
                    v += word + " ";
                }
                else if(word.length() > 0)
                    nv += word + " ";
                word = new String();
                break;
                default:
                word += ch;
            }
        }
        System.out.println("NUMBER OF WORDS BEGINNING AND ENDING WITH A VOWEL = " + count);
        System.out.println(v + nv);
    }
    public static boolean testWord(String w){
        int len = w.length();
        char first = w.charAt(0);
        char last = w.charAt(len - 1);
        if(isVowel(first) && isVowel(last))
            return true;
        return false;
    }
    public static boolean isVowel(char ch){
        switch(ch){
            case 'A':
            case 'E':
            case 'I':
            case 'O':
            case 'U':
            return true;
            default:
            return false;
        }
    }
}

Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.