Convert First Letter to Uppercase and Count Vowels Program in Java and Python
ISC 2015 Question 3 solution to convert the first letter of each word to uppercase and count vowels and consonants in every word.
Question:
Write a program to accept a sentence which may be terminated by either a full stop . or a question mark ? 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 properly formatted output.
Perform the following tasks:
- Convert the first letter of each word to uppercase.
- 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
Example 2
Example 3
Algorithm:
Step 1: Start.
Step 2: Create a Scanner object to accept a sentence from the user.
Step 3: Read the complete sentence in a string variable s.
Step 4: Find the last character of s.
Step 5: If the last character is neither . nor ?, display an invalid input message and go to Step 15.
Step 6: Create a StringTokenizer using space, full stop and question mark as delimiters.
Step 7: Count the number of words and create a string array to store them.
Step 8: For each word, store it in the array and convert its first character to uppercase.
Step 9: Add each converted word to the output sentence.
Step 10: Track the length of the longest word for formatted printing.
Step 11: Display the converted sentence.
Step 12: For each word, convert it to uppercase and count the characters that are A, E, I, O or U.
Step 13: Find consonants as word length - vowel count.
Step 14: Display each word with its vowel and consonant count using extra spaces for alignment.
Step 15: Stop.
Explanation:
The first important part of the program is input validation. The question allows only two terminating punctuation marks: a full stop or a question mark. Therefore the program checks the last character of the entered sentence before doing any further processing. If the sentence ends with any other character, such as an exclamation mark, it prints an error message. This keeps the program from treating invalid input as a normal sentence.
When the sentence is valid, the words are separated using StringTokenizer. The tokenizer uses the blank space, full stop and question mark as separators. This means the final punctuation mark is not included as part of the last word. Each token is stored in an array because the program needs to use the words twice: once to create the converted sentence and later to print the vowel and consonant table.
The conversion of each word is simple. The first character is extracted with charAt(0), changed to uppercase with Character.toUpperCase(), and then joined with the remaining part of the word using substring(1). This preserves the rest of the word exactly as entered. As each converted word is created, it is appended to a sentence string. The program also keeps the length of the longest word so that the table can be printed neatly.
For vowel counting, the word is first converted to uppercase. This allows the program to compare each character with only A, E, I, O and U instead of checking both uppercase and lowercase forms. Every matching character increases the vowel count. Since the question assumes normal words, the consonant count is found by subtracting the number of vowels from the length of the word. Finally, each word is padded with spaces using the helper method and printed with its number of vowels and consonants.
Java Program:
/**
* The class Q3_ISC2015 inputs a sentence, converts the first letter
* of each word to uppercase and counts vowels and consonants.
* @author : www.guideforschool.com
* @Program Type : BlueJ Program - Java
* @Question Year : ISC Practical 2015 Question 3
*/
import java.util.Scanner;
import java.util.StringTokenizer;
class Q3_ISC2015
{
int countVowel(String s)
{
s = s.toUpperCase();
int count = 0;
// Count all uppercase vowels in the word.
for(int i = 0; i < s.length(); i++)
{
char ch = s.charAt(i);
if(ch == 'A' || ch == 'E' || ch == 'I' || ch == 'O' || ch == 'U')
{
count++;
}
}
return count;
}
String convert(String s)
{
// Convert only the first character to uppercase.
char ch = s.charAt(0);
ch = Character.toUpperCase(ch);
return ch + s.substring(1);
}
String addSpace(String s, int max)
{
int extra = max - s.length();
// Add extra spaces to align all words in the table.
for(int i = 1; i <= extra; 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);
// The sentence must end with either a full stop or a question mark.
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 max = 0;
for(int i = 0; i < x; i++)
{
word[i] = str.nextToken();
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++)
{
int vow = ob.countVowel(word[i]);
int con = word[i].length() - vow;
y = ob.addSpace(word[i], max);
System.out.println(y + "\t" + vow + "\t" + con);
}
}
}
}Equivalent Python Program:
# Program to convert the first letter of each word to uppercase
# and count vowels and consonants in every word.
sentence = input("Enter a sentence : ")
last = sentence[-1]
if last != "." and last != "?":
print("Invalid Input. End a sentence with either '.' or '?'")
else:
# Remove the ending punctuation and split the sentence into words.
words = sentence[:-1].split(" ")
converted_words = []
max_length = 0
for word in words:
# Convert only the first character to uppercase.
converted = word[0].upper() + word[1:]
converted_words.append(converted)
if len(word) > max_length:
max_length = len(word)
print("Sentence =", " ".join(converted_words))
print("Word".ljust(max_length), "\tVowels\tConsonant")
for word in words:
vowel_count = 0
# Count vowels after converting the word to uppercase.
for ch in word.upper():
if ch in "AEIOU":
vowel_count += 1
consonant_count = len(word) - vowel_count
print(word.ljust(max_length), "\t", vowel_count, "\t", consonant_count, sep="")Output:
Leave a Reply
Your email address will not be published. Comments are reviewed before appearing publicly.