Click here to download the complete ISC 2015 Computer Science Paper 2 (Practical).
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 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
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.
Programming Code:
/** * The class Q3_ISC2015 inputs a sentence, converts the first letter of each word to * uppercase and find the number of vowels and consonants * @author : www.guideforschool.com * @Program Type : BlueJ Program - Java * @Question Year : ISC Practical 2015 Question 3 */ import java.util.*; class Q3_ISC2015 { int countVowel(String s) // Function to count no. of vowels in a word { s = s.toUpperCase(); int count = 0; char ch; for(int i=0; i<s.length(); i++) { ch = s.charAt(i); if(ch=='A' || ch=='E' || ch=='I' || ch=='O' || ch=='U') { count++; } } return count; } String convert(String s) // Function to convert 1st character to UpperCase { char ch = s.charAt(0); // Extracting the first character ch = Character.toUpperCase(ch); // Converting that character to UpperCase String f = ch + s.substring(1); // Adding it with the rest of the string return f; } String addSpace(String s, int max) // Function for addng extra space to make every word equal in length { int x = max-s.length(); for(int i=1; i<=x; i++) { s = s+" "; } return s; } public static void main(String args[]) { Q3_ISC2015 ob = new Q3_ISC2015(); Scanner sc=new Scanner(System.in); System.out.print("Enter a sentence : "); String s=sc.nextLine(); 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 != '?') { System.out.println("Invalid Input. End a sentence with either '.' or '?'"); } else { StringTokenizer str = new StringTokenizer(s," .?"); int x = str.countTokens(); String ans=""; String word[]=new String[x]; int vow, con, max=0; for(int i=0; i<x; i++) { word[i] = str.nextToken(); // Extracting words and saving them in an array ans = ans + " " + ob.convert(word[i]); if(word[i].length()>max) { max = word[i].length(); } } System.out.println("Sentence = "+ans.trim()); String y=ob.addSpace("Word",max); System.out.println(y+"\tVowels\tConsonant"); for(int i=0; i<x; i++) { vow = ob.countVowel(word[i]); con = word[i].length()-vow; // Consonant = Length - Vowel y = ob.addSpace(word[i],max); System.out.println(y+"\t"+vow+"\t"+con); } } } }
Output:
Enter a sentence : God is great.
Sentence = God Is Great
Word | Vowels | Consonants |
God | 1 | 2 |
Is | 1 | 1 |
Great | 2 | 3 |
Enter a sentence : All the best!
Invalid Input. End a sentence with either ‘.’ or ‘?’
Hello Sir!
In my written paper, there were a few mistakes, such as printing the output in the wrong order, i.e. the sentence after the vowel count and forgetting the throws statement. I made these changes in the practical, do you think marks would be deducted for this? And thank you for doing this, it’s really helpful!
No marks would be deducted.
Why is “t” printed in “tVowelstConsonants” and in (y+”t”+vow+”t”+con) ??
I wanted to thank you for the wonderful effort you’re taking. 🙂
It is “\t” for printing tab space.
I did everything correct and the program worked fine …. but I got incorrect output for just the first input eg. ,, others worked flawless ,,, how much marks will be deducted ?
No marks would be deducted.
Sir, instead of checking the last character as ‘.’ or ‘?’ by putting the condition if(last != ‘.’ && last !=’?’);
I have written the code as if(last != ‘.’ || last != ‘?’);.Code for Rest of the program is all same. how much marks will I lose for using the wrong symbol in the if condition??
Your written condition will make even a sentence with valid ending (like having ‘.’ or ‘?’) as invalid. But in the end, it all depends on the visiting examiner. He/she may overlook these mistakes.
Sir,
Ive attempted the first question. Ive successfuly run the exact program that i’ve written and have got the perfect output. Will i get full marks?
Yes
Sir I did not number question 3 .How many marks will be deducted?
Depends on the visiting examiner. He may or may not deduct marks. A maximum of 2 marks can be deducted.
Sir my program written on paper was not working so I wrote improved program on practical. Will I loose all my marks? Hoping for a reply. Thanks.
Not all, but yes few marks. But remember, it all depends on the visiting examiner.
Sir I’ve attempted question 3 …but slightly changed the code while doing it on the machine….will it create a problem?????……but my output was correct and in the given format…
No problem. You won’t lose marks if changes were minor.
sir the output is not in the format for sentence having more than 10 characters…
Yoshna, kindly copy the above program and re-run it.
–OUTPUT—–
Enter a sentence : intelligence plus character is education.
Sentence = Intelligence Plus Character Is Education
Word Vowels Consonant
intelligence 5 7
plus 1 3
character 3 6
is 1 1
education 5 4
(Even this program doesn’t give in proper format for big sentence…the question specifically mentioned output shud be in proper format..how to achieve so?)
Copy the above program and re-run it. Let me know if it is helpful.
Apologies sir ! yes now its working
Thankyou soo much !!
Did the same !
However didn’t get proper alignment and formatted output for example 1
Will there be any deductions?
* A 1000 eulogies in your name for helping us out *
No marks would be deducted.