by @kodeazy

flutter how to count the total number of consonants in a String?

Home » flutter » flutter how to count the total number of consonants in a String?

In this blog we will discuss on how to count the total number of consonants in a given string.
There are 26 alphabets in english language
Out of them 5 are vowels
i.e a,e,i,o,u
Remaining 21 are consonants.
I have a string as below.

  String str = "Counting consonant in a string";

To count the total number of consonants in the string
we create a variable and ,we Iterate through each and every letter of the string and check for the existence of the consonant.
If found then we increment the variable count.
Below is the example program to count the total number of vowels in a string.

void main() {
  String str = "Counting consonants in a string";
  str = str.toLowerCase();
  int consonantCount = 0;
  for (int i = 0; i < str.length; i++) {
    if (str[i] == 'a' ||
        str[i] == 'e' ||
        str[i] == 'i' ||
        str[i] == 'o' ||
        str[i] == 'u') {
      //Increments the vowel counter
      
    }
    else{
      consonantCount++;
    }
  }
  print("Total Number of consonant in given string is: $consonantCount");
}

output:

Total Number of consonant in given string is: 22