by @kodeazy

flutter how to count total number of vowels and consonants?

Home » flutter » flutter how to count total number of vowels and consonants?

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

To count the total number of vowels,consonants in a string
we create two variables vowelsCount,consonantCount and we iterate through each and every letter of the string and check for the existence of the consonant and vowels.
Below is the example program to count the total number of vowels in a string.

import 'dart:convert';
void main() {
  String str = "Vowels and Counting consonants in a string";
  str = str.toLowerCase();
  int consonantCount = 0;
  int vowelsCount = 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') {
      vowelsCount = vowelsCount + 1;
    } else {
      consonantCount++;
    }
  }
  print("Total Number of Vowels in given string is: $vowelsCount");
  print("Total Number of consonant in given string is: $consonantCount");
  }

Output

Total Number of Vowels in given string is: 12
Total Number of consonant in given string is: 30