Count consonants in a string (Iterative and recursive methods) in C++

C++Server Side ProgrammingProgramming

We are given a string with let’s say str of any length and the task is to calculate the count of consonants in the given string using both iterative and recursive methods.

Consonants are those alphabets that are not vowel i.e alphabets except a, i, e, o, u are considered as consonants. So in the program below we need to find the count of alphabets other than these in string.

The Recursion and Iteration both repeatedly execute the set of instructions. Recursion is when a statement in a function calls itself repeatedly. The iteration is when a loop repeatedly executes until the controlling condition becomes false. The primary difference between recursion and iteration is that recursion is a process, always applied to a function and iteration is applied to the set of instructions which we want to get repeatedly executed.

For Example

Input − string str = “tutorials point”
Output − count is 8

Explanation − In the given string str there are in total 8 consonants available and those are t, t, r, l, s, p, n and t.

Input − string str = “a e io u”
Output − count is 0

Explanation − In the given string str there is no consonant available instead it has only vowels so count is 0.

Iteration

Approach used in the below program is as follows

  • Input the string in a variable let’s say str

  • Calculate the length of the given string using the length() function that will return an integer value as per the number of characters in a string

  • Take a temporary variable that will store the count of elements.

  • Start loop for i to 0 till i less than the length of a string

  • Inside the loop, check IF str[i] is consonant then increment the value of count by 1

  • Return the count

  • Print the result.

Example

 Live Demo

// Iterative CPP program
#include <iostream>
using namespace std;
// Function to check for consonant
bool consonant(char ch){
   // To handle lower case
   ch = toupper(ch);
   return !(ch == 'A' || ch == 'E' || ch == 'I' || ch == 'O' || ch == 'U') && ch >= 65 && ch <= 90;
}
//function to count consonant
int countconsonants(string s){
   int result = 0;
   for (int i = 0; i < s.length(); i++){
      // To check is character is Consonant
      if (consonant(s[i])){
         ++result;
      }
   }
   return result;
}
// main function
int main(){
   string s = "wx abc def";
   cout <<"count is: "<<countconsonants(s);
   return 0;
}

Output

If we run the above code we will get the following output

count is: 6

Recursive

Approach used in the below program is as follows

  • Input the string in a variable let’s say str

  • Calculate the length of the given string using the length() function that will return an integer value as per the number of characters in a string

  • Take a temporary variable that will store the count of elements.

  • Create a recursive function that will call itself to calculate the consonants in a string

  • Check IF size is 1 then return str[0].

  • Then, return recursive_call_to_function as (str, size-1) + check for whether a character is string or not (str[size-1])

Example

 Live Demo

// Recursive CPP program
#include <iostream>
using namespace std;
// Function to check for consonant
bool consonant(char ch){
   // To convert the 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
int consonantcount(string str, int n){
   if (n == 1){
      return consonant(str[0]);
   }
   return consonantcount(str, n - 1) +
   consonant(str[n-1]);
}
int main(){
   string str = "wx abc def";
   cout <<"count is: "<<consonantcount(str, str.length());
   return 0;
}

Output

If we run the above code we will get the following output −

count is: 6
raja
Published on 15-May-2020 12:20:50
Advertisements