Challenge: Vowel Counter
Part of the Logic & Flow section of Coddy's Dart journey — lesson 64 of 65.
Challenge
EasyCreate a function called countVowels that analyzes text and counts the number of vowels it contains. This challenge combines string manipulation, loops, and conditional logic to solve a practical text processing problem.
Your task is to:
- Create a function named
countVowelsthat takes a singleStringparameter calledtext - The function should count all vowels in the text:
a,e,i,o, andu - The counting must be case-insensitive (both uppercase and lowercase vowels should be counted)
- The function should return an
intrepresenting the total number of vowels found - Read a string input from the user
- Call your
countVowelsfunction with the input string - Print the result with the exact format:
Vowel count: X(where X is the number of vowels)
Your function should iterate through each character in the string, convert it to lowercase for comparison, and check if it matches any of the five vowels. Use a counter variable to keep track of the total number of vowels found.
For example, if the input is "Hello World", your program should output:
Vowel count: 3If the input is "Programming", your program should output:
Vowel count: 3If the input is an empty string "", your program should output:
Vowel count: 0Your solution should demonstrate proper use of string iteration, conditional statements to check for vowel characters, and the toLowerCase() method to handle case-insensitive comparison.
Try it yourself
import 'dart:io';
// TODO: Create your countVowels function here
void main() {
// Read input
String? text = stdin.readLineSync();
// TODO: Call your countVowels function and store the result
// Output the result
print('Vowel count: $result');
}All lessons in Logic & Flow
1Advanced List Manipulation
List Properties: first & lastList State: isEmpty & isNotEmpReversing a ListAdding to a List: insertList Removal: removeWhereFinding in a List: indexOfSorting a ListShuffling a ListRecap - List Organizer4Advanced Map Manipulation
Iterating Over a MapChecking for Keys and ValuesMap Properties: keys & valuesConditional Add: putIfAbsentRemoving Entries from a MapNested MapsRecap - Inventory Update2Functional List Operations
Transforming with 'map'Filtering with 'where'Using '.toList()'Checking Conditions with 'any'Conditions with 'every'Finding with 'firstWhere'Recap - Data Filtering5Project: Shopping Cart Calc
Project SetupAdding Items to the Cart3Sets
What is a Set?Creating a SetAdding and Removing from SetsChecking for Elements in a SetConverting a List to a SetSet UnionSet IntersectionSet DifferenceRecap - Unique Guest List