C++ program to count vowels, consonant, digits and special characters in string using class

Given a string, we have to count vowels, consonant, digits and special characters in string using class the class and object approach.
Submitted by Shubh Pachori, on August 06, 2022

Example:

Input:
Enter String:shubh@123 amity@321

Output: 
Vowels: 3
Consonants: 7
Digits: 6
Spaces: 1
Special Character: 2

C++ code to count vowels, consonant, digits and special characters in string using class and object approach

#include <iostream>
using namespace std;

// create a class
class Count {
  // private data member
 private:
  char array[30];

  // public functions
 public:
  // putString() function for storing the string
  void putString() {
    cout << "Enter String:";

    // getline function is used to read the whole
    // string after spaces also
    cin.getline(array, 30);
  }

  // count() function to count all vowels, consonants,
  // digits, spaces and special Character in string
  void count() {
    // counters and indexing variables of type int
    int index, vowels = 0, consonants = 0, digits = 0, spaces = 0, special = 0;

    // for loop to access the whole string
    for (index = 0; array[index]; index++) {
      // if condition to check if the character is a alphabet or not
      if ((array[index] >= 'a' && array[index] <= 'z') ||
          (array[index] >= 'A' && array[index] <= 'Z')) {
        // if condition to check if the character is a vowel or not in both
        // cases
        if (array[index] == 'a' || array[index] == 'e' || array[index] == 'i' ||
            array[index] == 'o' || array[index] == 'u' || array[index] == 'A' ||
            array[index] == 'E' || array[index] == 'I' || array[index] == 'O' ||
            array[index] == 'U') {
          // if character is a vowel then counter will add 1
          vowels++;
        }

        // else it is a consonant so counter of consonant will add 1
        else {
          consonants++;
        }
      }

      // if it is a space
      else if (array[index] == ' ') {
        spaces++;
      }

      // if it is a digit
      else if (array[index] >= '0' && array[index] <= '9') {
        digits++;
      }

      // else it is a special character
      else {
        special++;
      }
    }

    // print all counted characters
    cout << "Vowels:" << vowels << "\n"
         << "Consonants:" << consonants << "\n"
         << "Digits:" << digits << "\n"
         << "Spaces:" << spaces << "\n"
         << "Special Character:" << special << endl;
  }
};

int main() {
  // create a object
  Count C;

  // calling putString() function to store the string
  C.putString();

  // calling count() function to count the characters
  C.count();

  return 0;
}

Output:

Enter String:amity@123 #shubh @pachori123
Vowels: 6
Consonants: 11
Digits: 6
Spaces: 2
Special Character: 3

Explanation:

In the above code, we have created a class Count, one char type array data member array[30] to store the string, and public member functions putString() and count() to store and count in the given string.

In the main() function, we are creating an object C of class Count, reading a string by the user using the putString() function, and finally calling the count() member function to count in the given string. The count() function contains the logic to count the vowels, consonants, digits, spaces, and special characters in the given string and printing the result.

C++ Class and Object Programs (Set 2) »




Comments and Discussions!










Copyright © 2023 www.includehelp.com. All rights reserved.