C++ Program to Find the Number of Vowels, Consonants, Digits and White Spaces in a String

Updated on December 5, 2024
Find the number of vowels, consonants, digits and white spaces in a string header image

Introduction

Analyzing a string to count various types of characters, such as vowels, consonants, digits, and white spaces, is a fundamental programming problem that can highlight important techniques in C++. This type of string analysis is not only useful for academic purposes but also serves as a good exercise for understanding iteration, condition checks, and character handling in C++.

In this article, you will learn how to craft a C++ program that efficiently counts the vowels, consonants, digits, and white spaces in a provided string. You will explore several examples that illustrate how to implement this functionality, ensuring you gain a clear understanding of handling strings and character types in C++.

Example Program to Analyze String Characters

Prepare the C++ Environment and Include Necessary Libraries

  1. Start by setting up your C++ programming environment. Any standard C++ compiler and a simple text editor will suffice.

  2. Include the iostream and ctype standard libraries, which will be essential for input/output operations and character-related functions.

    cpp
    #include <iostream>
    #include <cctype> // for isalpha, isdigit
    

Define the Main Function

  1. Write the main() function where the string input and operations will occur.

  2. Prompt the user to input a string.

    cpp
    int main() {
        std::string input;
        std::cout << "Enter a string: ";
        getline(std::cin, input);
    

Initialize Count Variables

  1. Initialize variables to hold the counts for vowels, consonants, digits, and spaces.

    cpp
    int vowels = 0, consonants = 0, digits = 0, spaces = 0;
    

Iterate Through the String and Count Characters

  1. Use a for loop to iterate through each character in the string.

  2. Apply conditions to classify and count each character type using isalpha, isdigit, and checking manually for spaces and vowel/consonant distinction.

  3. Display the final count of each character type upon completion of the loop.

    cpp
    for(char &c : input) {
            if (isspace(c)) {
                spaces++;
            } else if (isdigit(c)) {
                digits++;
            } else if (isalpha(c)) {
                if (tolower(c) == 'a' || tolower(c) == 'e' || tolower(c) == 'i' ||
                    tolower(c) == 'o' || tolower(c) == 'u') {
                    vowels++;
                } else {
                    consonants++;
                }
            }
        }
        std::cout << "Vowels: " << vowels << std::endl;
        std::cout << "Consonants: " << consonants << std::endl;
        std::cout << "Digits: " << digits << std::endl;
        std::cout << "White spaces: " << spaces << std::endl;
    
        return 0;
    }
    

    This code snippet efficiently categorizes each character in the string into vowels, consonants, digits, or spaces. Functions like isalpha and isspace assist in character classification to ensure accurate counting.

Conclusion

This C++ program demonstrates a practical approach to counting different types of characters in a string, featuring vowels, consonants, digits, and white spaces. By applying basic concepts such as for loops, conditionals, and character-specific functions, you effectively manage and analyze strings in a systematic manner. Adopt these techniques in various string processing tasks to enhance your coding skills and implement efficient character handling in your C++ projects.