Hedegare's Blog

Building - Learning - Optimizing - Growing

Algorithm

Beginner

Javascript

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

  1. Define a function that takes a string as an input parameter;
  2. Initialize a counter variable that keeps track of the number of vowels;
  3. Loop through the string and if a vowel is found, update the counter;
  4. 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.

index.js
1
function countVowels(str) {
2
3
}

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.

index.js
1
function countVowels(str) {
2
let counter = 0;
3
return counter;
4
}

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.

index.js
1
function countVowels(str) {
2
let counter = 0;
3
const vowels = "aeiou";
4
for (let character of str) {
5
if(vowels.includes(character)) counter++;
6
}
7
return counter;
8
}

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.

index.js
1
function countVowels(str) {
2
let counter = 0;
3
const vowels = "aeiou";
4
for (let character of str.toLowerCase()) {
5
if(vowels.includes(character)) counter++;
6
}
7
return counter;
8
}

We are now ready to test our function. You can use different inputs to ensure it works as expected. Here are a few examples.

index.js
1
// ... (previous code)
2
3
console.log(countVowels("hello world!")); // Output: 3
4
console.log(countVowels("Hedegare's Blog")); // Output: 5
5
console.log(countVowels("AEIOU are the vowels")); // Output: 10

The recap

We got to the end of our post; let’s recap everything we’ve done: