C++ Program to Count the Number of Vowels in a String

Introduction

In the world of programming, string manipulation is a fundamental skill that comes into play in various applications. One common task is to count the number of vowels in a given string. This article will guide you through creating a C++ Program to Count the Number of Vowels in a String. We’ll cover the prerequisites, provide multiple solutions with detailed explanations, and ensure that the content connects with you, the reader, on a personal level.

Prerequisites

Before diving into the code, it’s essential to have a basic understanding of the following concepts:

  1. C++ Programming Language: Familiarity with basic syntax, loops, and conditionals.
  2. String Handling: Understanding how strings work in C++ and common operations performed on them.
  3. Standard Library Functions: Knowledge of C++ Standard Library functions like toupper(), tolower(), etc.

With these prerequisites in mind, let’s explore various ways to count the number of vowels in a string using C++.

Example 1: Iterative Approach with Simple Loop

Code Explanation

In this example, we’ll use a simple loop to iterate through each character of the string and check if it is a vowel.

Code

C++
#include <iostream>
#include <string>
using namespace std;

int countVowels(const string& str) {
    int count = 0;
    for (char ch : str) {
        ch = tolower(ch); // Convert to lowercase
        if (ch == 'a' || ch == 'e' || ch == 'i' || ch == 'o' || ch == 'u') {
            count++;
        }
    }
    return count;
}

int main() {
    string input;
    cout << "Enter a string: ";
    getline(cin, input);
    int vowels = countVowels(input);
    cout << "Number of vowels in the string: " << vowels << endl;
    return 0;
}

Output

C++
Enter a string: Hello World
Number of vowels in the string: 3

Explanation

In this example, we convert each character to lowercase to handle both uppercase and lowercase vowels uniformly. The countVowels function iterates through each character and increments the count if the character is a vowel.

Example 2: Using Standard Library Functions

Code Explanation

In this example, we will use the C++ Standard Library algorithms to achieve the same result more concisely.

Code

C++
#include <iostream>
#include <string>
#include <algorithm>
#include <cctype>
using namespace std;

bool isVowel(char ch) {
    ch = tolower(ch);
    return (ch == 'a' || ch == 'e' || ch == 'i' || ch == 'o' || ch == 'u');
}

int countVowels(const string& str) {
    return count_if(str.begin(), str.end(), isVowel);
}

int main() {
    string input;
    cout << "Enter a string: ";
    getline(cin, input);
    int vowels = countVowels(input);
    cout << "Number of vowels in the string: " << vowels << endl;
    return 0;
}

Output

C++
Enter a string: Programming is fun!
Number of vowels in the string: 5

Explanation

Here, we use the count_if function from the <algorithm> library, which counts elements in the range that satisfy the isVowel predicate. This approach makes the code more readable and concise.

Example 3: Using Regular Expressions

Code Explanation

In this example, we will utilize regular expressions to find and count the vowels in a string.

Code

C++
#include <iostream>
#include <string>
#include <regex>
using namespace std;

int countVowels(const string& str) {
    regex vowelRegex("[aeiouAEIOU]");
    auto vowelsBegin = sregex_iterator(str.begin(), str.end(), vowelRegex);
    auto vowelsEnd = sregex_iterator();
    return distance(vowelsBegin, vowelsEnd);
}

int main() {
    string input;
    cout << "Enter a string: ";
    getline(cin, input);
    int vowels = countVowels(input);
    cout << "Number of vowels in the string: " << vowels << endl;
    return 0;
}

Output

C++
Enter a string: Regular expressions are powerful!
Number of vowels in the string: 11

Explanation

In this example, we use the <regex> library to create a regular expression that matches vowels. We then use sregex_iterator to iterate over all matches and count them. This method leverages the power of regular expressions to simplify the vowel counting process.

Conclusion

Counting the number of vowels in a string is a common problem with multiple solutions in C++. We’ve explored three different approaches: a simple iterative method, utilizing standard library functions, and employing regular expressions. Each method has its own advantages and can be chosen based on the specific requirements of the task at hand. By understanding and implementing these solutions, you enhance your string manipulation skills in C++, which are crucial for many programming tasks