Count consonants in a string (Iterative and recursive methods)

Given a string, count total number of consonants in it. A consonant is a English alphabet character that is not vowel (a, e, i, o and u). Examples of constants are b, c, d, f, g, ..

Examples :

Input : abc de
Output : 3
There are three consonants b, c and d.

Input : geeksforgeeks portal
Output : 12



1. Iterative Method

C++

// Iterative CPP program to count total number
// of consonants 
#include <iostream>
using namespace std;

// Function to check for consonant
bool isConsonant(char ch)
{
    // To handle lower case
    ch = toupper(ch);

    return !(ch == 'A' || ch == 'E' || 
            ch == 'I' || ch == 'O' || 
            ch == 'U') && ch >= 65 && ch <= 90;
}

int totalConsonants(string str)
{
    int count = 0;
    for (int i = 0; i < str.length(); i++) 

        // To check is character is Consonant
        if (isConsonant(str[i]))
            ++count;
    return count;
}

// Driver code
int main()
{
    string str = "abc de";
    cout << totalConsonants(str);
    return 0;
}

Java

// Iterative Java program
// to count total number
// of consonants 

import java.io.*;

class GFG {

    // Function to check for consonant
    static boolean isConsonant(char ch)
    {
        // To handle lower case
        ch = Character.toUpperCase(ch);
     
        return !(ch == 'A' || ch == 'E' || 
                ch == 'I' || ch == 'O' || 
                ch == 'U') && ch >= 65 && ch <= 90;
    }
 
    static int totalConsonants(String str)
    {
        int count = 0;
        for (int i = 0; i < str.length(); i++) 
     
            // To check is character is Consonant
            if (isConsonant(str.charAt(i)))
                ++count;
        return count;
    }
    
    // Driver code
    public static void main(String args[])
    {
        String str = "abc de";
        System.out.println( totalConsonants(str));
    }
}

// This code is contributed by Nikita Tiwari.

Python3

# Iterative Python3 program to count 
# total number of consonants 

# Function to check for consonant
def isConsonant(ch):
    
    # To handle lower case
    ch = ch.upper()

    return not (ch == 'A' or ch == 'E' or 
                ch == 'I' or ch == 'O' or 
                ch == 'U') and ord(ch) >= 65 and ord(ch) <= 90

def totalConsonants(string):
    
    count = 0
    
    for i in range(len(string)):

        # To check is character is Consonant
        if (isConsonant(string[i])):
            count += 1
            
    return count

# Driver code
string = "abc de"
print(totalConsonants(string))


# This code id contributed by Ansu Kumari.

C#

// Iterative C# program to count 
// total number of consonants 
using System;

class GFG {

    // Function to check for consonant
    static bool isConsonant(char ch)
    {
        // To handle lower case
        ch = Char.ToUpper(ch);
    
        return !(ch == 'A' || ch == 'E' || 
                 ch == 'I' || ch == 'O' || 
                 ch == 'U') && ch >= 65 && ch <= 90;
    }

    static int totalConsonants(String str)
    {
        int count = 0;
        for (int i = 0; i < str.Length; i++) 
    
            // To check is character is Consonant
            if (isConsonant(str[i]))
                ++count;
        return count;
    }
    
    // Driver code
    public static void Main()
    {
        String str = "abc de";
        Console.Write( totalConsonants(str));
    }
}

// This code is contributed by nitin mittal.


Output:

3

2. Recursive Method

C++

// Recursive CPP program to count total number
// of consonants 
#include <iostream>
using namespace std;

// Function to check for consonant
bool isConsonant(char ch)
{
    // To handle lower case
    ch = toupper(ch);

    return !(ch == 'A' || ch == 'E' || 
            ch == 'I' || ch == 'O' || 
            ch == 'U') && ch >= 65 && ch <= 90;
}

// to count total number of consonants from 
// 0 to n-1
int totalConsonants(string str, int n)
{
    if (n == 1)
        return isConsonant(str[0]);

    return totalConsonants(str, n - 1) + 
           isConsonant(str[n-1]);
}

// Driver code
int main()
{
    string str = "abc de";
    cout << totalConsonants(str, str.length());
    return 0;
}

Python3

# Recursive Python3 program to count 
# total number of consonants 

# Function to check for consonant
def isConsonant(ch):
    
    # To handle lower case
    ch = ch.upper()

    return not (ch == 'A' or ch == 'E' or 
                ch == 'I' or ch == 'O' or 
                ch == 'U') and ord(ch) >= 65 and ord(ch) <= 90

# To count total number of  
# consonants from 0 to n-1
def totalConsonants(string, n):
    
    if n == 1:
        return isConsonant(string[0])

    return totalConsonants(string, n - 1) + isConsonant(string[n-1])


# Driver code
string = "abc de"
print(totalConsonants(string, len(string)))


# This code is contributed by Ansu Kuamri.


Output :

3

Illustration of recursive method:



My Personal Notes arrow_drop_up

Check out this Author's contributed articles.

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please write comments if you find anything incorrect, or you want to share more information about the topic discussed above.


Improved By : nitin mittal




Practice Tags :

Recommended Posts:



1.5 Average Difficulty : 1.5/5.0
Based on 2 vote(s)






User Actions