Skip to content Skip to sidebar Skip to footer

Easy Way to Tell How Many Consonants Are in a Word

Count consonants in a string (Iterative and recursive methods)

View Discussion

Improve Article

Save Article

Like Article

  • Read
  • Discuss
  • View Discussion

    Improve Article

    Save Article

    Like Article

    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++

    #include <iostream>

    using namespace std;

    bool isConsonant( char ch)

    {

    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++)

    if (isConsonant(str[i]))

    ++count;

    return count;

    }

    int main()

    {

    string str = "abc de" ;

    cout << totalConsonants(str);

    return 0;

    }

    Java

    import java.io.*;

    class GFG {

    static boolean isConsonant( char ch)

    {

    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++)

    if (isConsonant(str.charAt(i)))

    ++count;

    return count;

    }

    public static void main(String args[])

    {

    String str = "abc de" ;

    System.out.println( totalConsonants(str));

    }

    }

    Python3

    def isConsonant(ch):

    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)):

    if (isConsonant(string[i])):

    count + = 1

    return count

    string = "abc de"

    print (totalConsonants(string))

    C#

    using System;

    class GFG {

    static bool isConsonant( char ch)

    {

    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++)

    if (isConsonant(str[i]))

    ++count;

    return count;

    }

    public static void Main()

    {

    String str = "abc de" ;

    Console.Write( totalConsonants(str));

    }

    }

    PHP

    <?php

    function isConsonant( $ch )

    {

    $ch = strtoupper ( $ch );

    return !( $ch == 'A' || $ch == 'E' ||

    $ch == 'I' || $ch == 'O' ||

    $ch == 'U' ) && ord( $ch ) >= 65 && ord( $ch ) <= 90;

    }

    function totalConsonants( $str )

    {

    $count = 0;

    for ( $i = 0; $i < strlen ( $str ); $i ++)

    if (isConsonant( $str [ $i ]))

    ++ $count ;

    return $count ;

    }

    $str = "abc de" ;

    echo totalConsonants( $str );

    return 0;

    ?>

    Javascript

    <script>

    function isConsonant(ch)

    {

    ch = ch.toUpperCase();

    console.log(ch);

    return (

    !(ch == "A" || ch == "E" || ch == "I" || ch == "O" || ch == "U" ) &&

    ch.match(/[A-Z]/i)

    );

    }

    function totalConsonants(str) {

    var count = 0;

    for ( var i = 0; i < str.length; i++)

    if (isConsonant(str[i])) ++count;

    return count;

    }

    var str = "abc de" ;

    document.write(totalConsonants(str));

    </script>

    Time Complexity: O(n), where n is the length of the string
    Auxiliary Space: O(1)

    2. Recursive Method

    C++

    #include <iostream>

    using namespace std;

    bool isConsonant( char ch)

    {

    ch = toupper (ch);

    return !(ch == 'A' || ch == 'E' ||

    ch == 'I' || ch == 'O' ||

    ch == 'U' ) && ch >= 65 && ch <= 90;

    }

    int totalConsonants(string str, int n)

    {

    if (n == 1)

    return isConsonant(str[0]);

    return totalConsonants(str, n - 1) +

    isConsonant(str[n-1]);

    }

    int main()

    {

    string str = "abc de" ;

    cout << totalConsonants(str, str.length());

    return 0;

    }

    Java

    import java.util.*;

    import java.lang.*;

    class GFG

    {

    static boolean isConsonant( char ch)

    {

    ch = Character.toUpperCase(ch);

    return (ch == 'A' || ch == 'E' ||

    ch == 'I' || ch == 'O' ||

    ch == 'U' )== false && ch >= 65 && ch <= 90 ;

    }

    static int totalConsonants(String str, int n)

    {

    if (n == 1 )

    {

    if (isConsonant(str.charAt( 0 )))

    return 1 ;

    else

    return 0 ;

    }

    if (isConsonant(str.charAt(n - 1 )))

    return totalConsonants(str, n - 1 ) + 1 ;

    else

    return totalConsonants(str, n - 1 );

    }

    public static void main(String args[])

    {

    String str = "abc de" ;

    System.out.println(totalConsonants(str, str.length()));

    }

    }

    Python3

    def isConsonant(ch):

    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, n):

    if n = = 1 :

    return isConsonant(string[ 0 ])

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

    string = "abc de"

    print (totalConsonants(string, len (string)))

    C#

    using System;

    class GFG

    {

    static Boolean isConsonant( char ch)

    {

    ch = char .ToUpper(ch);

    return (ch == 'A' || ch == 'E' ||

    ch == 'I' || ch == 'O' ||

    ch == 'U' ) == false &&

    ch >= 65 && ch <= 90;

    }

    static int totalConsonants(String str, int n)

    {

    if (n == 1)

    {

    if (isConsonant(str[0]))

    return 1;

    else

    return 0;

    }

    if (isConsonant(str[n - 1]))

    return totalConsonants(str, n - 1) + 1;

    else

    return totalConsonants(str, n - 1);

    }

    public static void Main(String []args)

    {

    String str = "abc de" ;

    Console.WriteLine(totalConsonants(str, str.Length));

    }

    }

    Javascript

    <script>

    function isConsonant(ch)

    {

    ch = ch.toUpperCase();

    return (!(ch == 'A' || ch == 'E' ||

    ch == 'I' || ch == 'O' ||

    ch == 'U' ) && ch.charCodeAt(0) >= 65 &&

    ch.charCodeAt(0) <= 90) ;

    }

    function totalConsonants(str, n)

    {

    if (n == 1)

    return isConsonant(str[0]);

    return totalConsonants(str, n - 1) +

    isConsonant(str[n - 1]);

    }

    var str = "abc de" ;

    document.write(totalConsonants(str,str.length));

    </script>

    Time Complexity: O(n), where n is the length of the string
    Auxiliary Space: O(1)

    Illustration of recursive method:


    mooreweept1962.blogspot.com

    Source: https://www.geeksforgeeks.org/count-consonants-string-iterative-recursive-methods/

    Post a Comment for "Easy Way to Tell How Many Consonants Are in a Word"