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

Updated on 15 May, 2025
Find the number of vowels, consonants, digits and white spaces in a string header image

Introduction

Counting vowels, consonants, digits, and white spaces in a string using C++ is a common programming problem that helps developers practice essential skills. This type of string character analysis is not only valuable for beginners learning C++ but also demonstrates key programming concepts such as loops, conditional statements, and character handling. Whether for academic exercises or real-world applications, analyzing strings in this way reinforces core C++ string manipulation techniques.

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

C++ Program to Count Vowels, Consonants, Digits, and White Spaces in a String

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;
    

###Loop Through the String to Count Vowels, Consonants, Digits, and White Spaces

  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.

Comments

No comments yet.