[Question 3] ISC 2016 Computer Practical Paper Solved – Words Beginning and Ending with Vowel
ISC 2016 vowel words program solved with algorithm, explanation, Java program and Python code.
Click here to download the complete ISC 2016 Computer Science Paper 2 (Practical).
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 and ending with a vowel.
(b) 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
Algorithm:
Step 1: Start.
Step 2: Input a sentence and convert it to uppercase.
Step 3: Check the last character of the sentence.
Step 4: If the sentence does not end with ., ? or !, display an invalid input message and stop.
Step 5: Split the sentence into words by ignoring punctuation marks and spaces.
Step 6: For each word, check whether its first and last characters are vowels.
Step 7: Store vowel-start-and-end words in one string and the remaining words in another string.
Step 8: Count the words that begin and end with a vowel.
Step 9: Display the count, followed by the rearranged sentence with matching words first.
Step 10: Stop.
Explanation:
The program rearranges the words of a sentence according to one condition: words that begin and end with a vowel must appear before the other words. The first validation checks whether the sentence ends with a proper terminating punctuation mark. This is important because the question expects a complete sentence ending with a full stop, question mark or exclamation mark. After validation, the sentence is converted to uppercase so that vowel checking becomes simple and consistent.
The program separates the sentence into words and checks each word individually. A word is counted when its first character and last character are both among A, E, I, O and U. Such words are appended to one result string, while all other words are appended to a second result string. At the end, the count is displayed and the two strings are joined, placing the matching vowel words first. This preserves the relative order within each group and produces the exact type of rearranged output required by the ISC question.
In the sample sentence, ANAMIKA, ARE and ANYMORE begin and end with vowels, so they are counted and stored first. Words such as AND and SUSAN do not satisfy both conditions, so they are stored in the second group. The punctuation mark is used only for validation and is not included in the rearranged output. This makes the word checking simple because each token contains only letters.
Programming Code:
/**
* The class ISC2016_Q3 inputs a sentence, and prints and counts the words
* beginning and ending with a vowel, before other words
* @author : www.guideforschool.com
* @Program Type : BlueJ Program - Java
* @Question Year : ISC Practical 2016 Question 3
*/
import java.util.*;
class ISC2016_Q3
{
boolean isVowel(String w) // Function to check if a word begins and ends with a vowel or not
{
int l = w.length();
char ch1 = w.charAt(0); // Storing the first character
char ch2 = w.charAt(l-1); // Storing the last character
if((ch1=='A' || ch1=='E' || ch1=='I' || ch1=='O' || ch1=='U') &&
(ch2=='A' || ch2=='E' || ch2=='I' || ch2=='O' || ch2=='U'))
{
return true;
}
else
{
return false;
}
}
public static void main(String args[])
{
ISC2016_Q3 ob = new ISC2016_Q3();
Scanner sc = new Scanner(System.in);
System.out.print("Enter a sentence : ");
String s = sc.nextLine();
s = s.toUpperCase();
int l = s.length();
char last = s.charAt(l-1); // Extracting the last character
/* Checking whether the sentence ends with '.' or '?' or not */
if(last != '.' && last != '?' && last != '!')
{
System.out.println("Invalid Input. End a sentence with either '.', '?' or '!' only");
}
else
{
StringTokenizer str = new StringTokenizer(s," .?!");
int x = str.countTokens();
int c = 0;
String w = "", a = "", b = "";
for(int i=1; i<=x; i++)
{
w = str.nextToken(); // Extracting words and saving them in w
if(ob.isVowel(w))
{
c++; // Counting all words beginning and ending with a vowel
a = a + w + " "; // Saving all words beginning and ending with a vowel in variable 'a'
}
else
b = b + w + " "; // Saving all other words in variable 'b'
}
System.out.println("OUTPUT : \nNUMBER OF WORDS BEGINNING AND ENDING WITH A VOWEL = " + c);
System.out.println(a+b);
}
}
}Equivalent Python Program:
def begins_and_ends_with_vowel(word):
vowels = "AEIOU"
return word[0] in vowels and word[-1] in vowels
sentence = input("Enter a sentence : ").upper()
last = sentence[-1]
if last not in ".?!":
print("Invalid Input. End a sentence with either '.', '?' or '!' only")
else:
cleaned = sentence[:-1]
words = cleaned.split()
vowel_words = []
other_words = []
for word in words:
if begins_and_ends_with_vowel(word):
vowel_words.append(word)
else:
other_words.append(word)
print("OUTPUT :")
print("NUMBER OF WORDS BEGINNING AND ENDING WITH A VOWEL =", len(vowel_words))
print(*(vowel_words + other_words))Output:
Enter a sentence : 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
Leave a Reply
Your email address will not be published. Comments are reviewed before appearing publicly.