ISC practical papers › ISC 2016 › Question 3
Words beginning/ending with vowels
ISC 2016 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 may be separated by more than one blank space and are in UPPER CASE.
Perform the following tasks: a) Find the number of words beginning an ending with a vowel. b) Place the words which begin and 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 TOMMORROW THAN YOU TODAY
Example 3
OUTPUT: NUMBER OF WORDS BEGINNING AND ENDING WITH A VOWEL = 0
LOOK BEFORE YOU LEAP
Example 4 INPUT: HOW ARE YOU@ OUTPUT: INVALID INPUT
Question: ISC 2016 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
import java.util.*;
class Sentence {
private String sentence, answer;
private static String vowels = "AEIOUaeiou";
public Sentence(String sentence) {
this.sentence = sentence;
answer = "";
}
private static boolean isVowel(char ch) {
return vowels.indexOf(ch) >= 0;
}
public int countWordsLeadingAndTrailingVowels() {
int count = 0;
String sentenceCopy = sentence.substring(0, sentence.length() - 1);
String wordList[] = sentenceCopy.split(" ");
String beginingPart = "", endingPart = "";
for (String word : wordList) {
if (word.length() == 0)
continue;
if (isVowel(word.charAt(0)) && isVowel(word.charAt(word.length() - 1))) {
beginingPart += word + " ";
count++;
} else {
endingPart += word + " ";
}
}
answer = beginingPart + endingPart;
return count;
}
public boolean isValid() {
String terminators = ".?!";
return terminators.indexOf(sentence.charAt(sentence.length() - 1)) >= 0;
}
public String toString() {
return answer.trim();
}
}
class ISC2016Q3 {
public static void main(String[] args) {
Scanner in = new Scanner(System.in);
System.out.print("INPUT:\t");
String source = in.nextLine();
Sentence sentence = new Sentence(source);
if (sentence.isValid()) {
int count = sentence.countWordsLeadingAndTrailingVowels();
System.out.println("OUTPUT:\tNUMBER OF WORDS BEGINNING AND ENDING WITH A VOWEL = " + count);
System.out.println("\t" + sentence);
} else {
System.out.println("OUTPUT:\tINVALID 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.