Hedegare's Blog
Building - Learning - Optimizing - Growing
How to Create a Function to Count Vowels in a String
Counting characters in a string is a common task in programming that can be both educational and useful. In this tutorial, we’ll be more specific and will walk through how to create a function to count the number of vowels in a given string. This guide is perfect for beginners and anyone looking to refresh their coding skills.
We’ll start by identifying the problem. Our function will be receiving a string, count how many characters a, e, i, o and u are present and return that value. Seems easy, so let’s get to work.
The plan
- Define a function that takes a string as an input parameter;
- Initialize a counter variable that keeps track of the number of vowels;
- Loop through the string and if a vowel is found, update the counter;
- Return the count value.
The implementation
I chose to use Javascript to solve this problem. You can use another language;; although the code will differ, the logic remains the same.
Begin by creating a JavaScript file. (I named mine index.js but you can name yours however you want.)
Then, let’s start by step 1 of our plan; defining a function that takes a string as an input. Write the following code into your file.
function countVowels(str) {
}We can implement steps 2 and 4 at the same time, since both involve the counter variable. Initialize a variable called counter with a value of 0 and then return it.
function countVowels(str) { let counter = 0; return counter;}Now we get to the best part, step 3.
First we’ll identify the characters we want to find (in our case are the vowels aeiou) and assign them to a variable vowels.
Then, since a string in Javascript is iterable, we can loop through each character in the input string and check if that character exists in vowels.
function countVowels(str) { let counter = 0; const vowels = "aeiou"; for (let character of str) { if(vowels.includes(character)) counter++; } return counter;}The implemention of the function is almost complete; we just need to modify it for an edge case. What if the input string contains uppercase characters? The solution is simple: convert every character of the string to lowercase using the toLowerCase method. Update the following highlighted line.
function countVowels(str) { let counter = 0; const vowels = "aeiou"; for (let character of str.toLowerCase()) { if(vowels.includes(character)) counter++; } return counter;}We are now ready to test our function. You can use different inputs to ensure it works as expected. Here are a few examples.
// ... (previous code)
console.log(countVowels("hello world!")); // Output: 3console.log(countVowels("Hedegare's Blog")); // Output: 5console.log(countVowels("AEIOU are the vowels")); // Output: 10The recap
We got to the end of our post; let’s recap everything we’ve done:
- Define the function: we defined a function called
countVowelswith astrparameter; - Initialize the counter: we started with a vowel
counterset to 0; - Define the vowels: we defined a string containing all the vowels for easy comparison;
- Convert to lowercase: before iterating through the string, we converted it to lowercase to ensure uppercase characters are counted;
- Iterate through the string: we used a
for ... ofloop to iterate through each character in the string; - Check and count: we checked if the character is a vowel by using the
includesmethod and incremented the counter if it is; - Return count: finally, we returned the value of the
counter.
Last updated on 2025-06-01