by @kodeazy

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

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

In this blog we will discuss on how to count the total number of vowels 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 vowels in a string";

To count the total number of vowels 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 vowel.
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 vowels in a string";
  str = str.toLowerCase();
  int vowelCount = 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
      vowelCount++;
    }
  }
  print("Total Number of vowels in given string is: $vowelCount");
}

output:

Total Number of vowels in given string is: 8