Count consonants in a string (Iterative and recursive methods)
Last Updated :
20 Feb, 2023
Given a string, count total number of consonants in it. A consonant is an English alphabet character that is not vowel (a, e, i, o and u). Examples of constants are b, c, d, f, and 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.
PHP
<?php
// Iterative PHP program to count total number
// of consonants
// Function to check for consonant
function isConsonant($ch)
{
// To handle lower case
$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++)
// To check is character is Consonant
if (isConsonant($str[$i]))
++$count;
return $count;
}
// Driver code
$str = "abc de";
echo totalConsonants($str);
return 0;
// This code is contributed by Ita_c.
?>
JavaScript
<script>
// Iterative JavaScript program to count total number
// of consonants
// Function to check for consonant
function isConsonant(ch)
{
// To handle lower case
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++)
// To check is character is Consonant
if (isConsonant(str[i])) ++count;
return count;
}
// Driver code
var str = "abc de";
document.write(totalConsonants(str));
// This code is contributed by rdtank.
</script>
Time Complexity: O(n), where n is the length of the string
Auxiliary Space: O(1)
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;
}
Java
// Recursive Java program to count
// total number of consonants
import java.util.*;
import java.lang.*;
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')== false && ch >= 65 && ch <= 90;
}
// to count total number
// of consonants from 0 to n-1
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);
}
// Driver code
public static void main(String args[])
{
String str = "abc de";
System.out.println(totalConsonants(str, str.length()));
}
}
// This code is contributed by
// Surendra_Gangwar
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.
C#
// Recursive C# program to count
// total number of consonants
using System;
class GFG
{
// Function to check for consonant
static Boolean isConsonant(char ch)
{
// To handle lower case
ch = char.ToUpper(ch);
return (ch == 'A' || ch == 'E' ||
ch == 'I' || ch == 'O' ||
ch == 'U') == false &&
ch >= 65 && ch <= 90;
}
// to count total number
// of consonants from 0 to n-1
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);
}
// Driver code
public static void Main(String []args)
{
String str = "abc de";
Console.WriteLine(totalConsonants(str, str.Length));
}
}
// This code contributed by Rajput-Ji
JavaScript
<script>
// Recursive Javascript program to
// count total number of consonants
// Function to check for consonant
function isConsonant(ch)
{
// To handle lower case
ch = ch.toUpperCase();
return (!(ch == 'A' || ch == 'E' ||
ch == 'I' || ch == 'O' ||
ch == 'U') && ch.charCodeAt(0) >= 65 &&
ch.charCodeAt(0) <= 90) ;
}
// To count total number of consonants from
// 0 to n-1
function totalConsonants(str, n)
{
if (n == 1)
return isConsonant(str[0]);
return totalConsonants(str, n - 1) +
isConsonant(str[n - 1]);
}
// Driver code
var str = "abc de";
document.write(totalConsonants(str,str.length));
// This code is contributed by jana_sayantan
</script>
Time Complexity: O(n), where n is the length of the string
Auxiliary Space: O(n), due to recursive call stacks.
Illustration of recursive method:

Count consonants in a string
Similar Reads
Count occurrences of a character in a repeated string Given an integer N and a lowercase string. The string is repeated infinitely. The task is to find the No. of occurrences of a given character x in first N letters.Examples: Input : N = 10 str = "abcac"Output : 4Explanation: "abcacabcac" is the substring from the infinitely repeated string. In first
8 min read
Count strings with consonants and vowels at alternate position Given a string str. The task is to find all possible number of strings that can be obtained by replacing the "$" with alphabets in the given string. Note: Alphabets should be placed in such a way that the string is always alternating in vowels and consonants, and the string must always start with a
6 min read
Count Occurrences of a Given Character in a String Given a string S and a character 'c', the task is to count the occurrence of the given character in the string.Examples: Input : S = "geeksforgeeks" and c = 'e'Output : 4Explanation: 'e' appears four times in str.Input : S = "abccdefgaa" and c = 'a' Output : 3Explanation: 'a' appears three times in
6 min read
Recursive solution to count substrings with same first and last characters We are given a string S, we need to find count of all contiguous substrings starting and ending with same character. Examples : Input : S = "abcab" Output : 7 There are 15 substrings of "abcab" a, ab, abc, abca, abcab, b, bc, bca bcab, c, ca, cab, a, ab, b Out of the above substrings, there are 7 su
11 min read
Count substrings with same first and last characters Given a string s consisting of lowercase characters, the task is to find the count of all substrings that start and end with the same character.Examples : Input : s = "abcab"Output : 7Explanation: The substrings are "a", "abca", "b", "bcab", "c", "a", "b".Input : s = "aba"Output : 4Explanation: The
7 min read
Count substrings with same first and last characters Given a string s consisting of lowercase characters, the task is to find the count of all substrings that start and end with the same character.Examples : Input : s = "abcab"Output : 7Explanation: The substrings are "a", "abca", "b", "bcab", "c", "a", "b".Input : s = "aba"Output : 4Explanation: The
7 min read