Write a program to count the number of consonants in a given word. Consonants are the letters of the English alphabet excluding vowels ('a', 'e', 'i', 'o', 'u').
Examples:
Input: "programming"
Output: 8
Explanation: The eight consonants are: 'p', 'r', 'g', 'r', 'm', 'm', 'n', 'g')Input: "hello"
Output: 3
Explanation: There are three consonants: 'h', 'l', 'l'
Approach: To solve the problem, follow the below idea:
The problem can be solved by iterating through the entire word character by character and for each character, check if the character is a vowel. If the character is 'a' or 'e' or 'i' or 'o' or 'u', then it is a vowel otherwise it is a consonant. If the character is a consonant, increment the answer by 1.
Step-by-step algorithm:
- Initialize a variable to store the count of consonants.
- Iterate through each character in the word.
- Check if the character is a consonant (not a vowel).
- If it is a consonant, increment the count.
- Print the final count.
Below is the implementation of the approach:
#include <iostream>
using namespace std;
// function to check whether ch is a lowercase alphabet or
// not
bool isLowerCaseAlphabet(char ch)
{
return ch >= 'a' && ch <= 'z';
}
// function to check whether ch is a lowercase vowel or not
bool isVowel(char ch)
{
return ch == 'a' || ch == 'e' || ch == 'i' || ch == 'o'
|| ch == 'u';
}
int main()
{
string word = "programming";
int consonantCount = 0;
for (int i = 0; i < word.length(); ++i) {
// Convert to lowercase for case-insensitivity
char ch = tolower(word[i]);
if (isLowerCaseAlphabet(ch) && !isVowel(ch)) {
consonantCount++;
}
}
cout << "Number of consonants: " << consonantCount
<< endl;
return 0;
}
#include <stdio.h>
// function to check whether ch is a lowercase alphabet or not
int isLowerCaseAlphabet(char ch)
{
return (ch >= 'a' && ch <= 'z');
}
// function to check whether ch is a lowercase vowel or not
int isVowel(char ch)
{
return (ch == 'a' || ch == 'e' || ch == 'i' || ch == 'o'
|| ch == 'u');
}
int main()
{
char word[] = "programming";
int consonantCount = 0;
for (int i = 0; i < strlen(word); ++i) {
// Convert to lowercase for case-insensitivity
char ch = tolower(word[i]);
if (isLowerCaseAlphabet(ch) && !isVowel(ch)) {
consonantCount++;
}
}
printf("Number of consonants: %d\n", consonantCount);
return 0;
}
public class ConsonantCounter {
// function to check whether ch is a lowercase alphabet
// or not
static boolean isLowerCaseAlphabet(char ch)
{
return ch >= 'a' && ch <= 'z';
}
// function to check whether ch is a lowercase vowel or
// not
static boolean isVowel(char ch)
{
return ch == 'a' || ch == 'e' || ch == 'i'
|| ch == 'o' || ch == 'u';
}
public static void main(String[] args)
{
String word = "programming";
int consonantCount = 0;
for (int i = 0; i < word.length(); ++i) {
// Convert to lowercase for case-insensitivity
char ch = Character.toLowerCase(word.charAt(i));
if (isLowerCaseAlphabet(ch) && !isVowel(ch)) {
consonantCount++;
}
}
System.out.println("Number of consonants: "
+ consonantCount);
}
}
def is_lower_case_alphabet(ch):
return 'a' <= ch <= 'z'
def is_vowel(ch):
return ch in {'a', 'e', 'i', 'o', 'u'}
word = "programming"
consonant_count = 0
for i in range(len(word)):
# Convert to lowercase for case-insensitivity
ch = word[i].lower()
if is_lower_case_alphabet(ch) and not is_vowel(ch):
consonant_count += 1
print("Number of consonants:", consonant_count)
using System;
class Program {
// Function to check whether ch is a lowercase alphabet
// or not
static bool IsLowerCaseAlphabet(char ch)
{
return ch >= 'a' && ch <= 'z';
}
// Function to check whether ch is a lowercase vowel or
// not
static bool IsVowel(char ch)
{
return ch == 'a' || ch == 'e' || ch == 'i'
|| ch == 'o' || ch == 'u';
}
static void Main()
{
string word = "programming";
int consonantCount = 0;
for (int i = 0; i < word.Length; ++i) {
// Convert to lowercase for case-insensitivity
char ch = char.ToLower(word[i]);
if (IsLowerCaseAlphabet(ch) && !IsVowel(ch)) {
consonantCount++;
}
}
Console.WriteLine("Number of consonants: "
+ consonantCount);
}
}
// Function to check whether ch is a lowercase alphabet or not
function isLowerCaseAlphabet(ch) {
return ch >= 'a' && ch <= 'z';
}
// Function to check whether ch is a lowercase vowel or not
function isVowel(ch) {
return ch === 'a' || ch === 'e' || ch === 'i' || ch === 'o' || ch === 'u';
}
let word = "programming";
let consonantCount = 0;
for (let i = 0; i < word.length; ++i) {
// Convert to lowercase for case-insensitivity
let ch = word[i].toLowerCase();
if (isLowerCaseAlphabet(ch) && !isVowel(ch)) {
consonantCount++;
}
}
console.log("Number of consonants: " + consonantCount);
Output
Number of consonants: 8
Time Complexity: O(N) where N is the length of the word.
Auxiliary Space: O(1) (constant space is used)