BeginnersBook

  • Home
  • Java
    • Java OOPs
    • Java Collections
    • Java Examples
  • C
    • C Examples
  • C++
    • C++ Examples
  • DBMS
  • Computer Network
  • Python
    • Python Examples
  • More…
    • jQuery
    • Kotlin
    • WordPress
    • SEO
    • JSON
    • JSP
    • JSTL
    • Servlet
    • MongoDB
    • XML
    • Perl

C++ Program to find number of Vowels and Consonants in a String

Last Updated: September 12, 2017 by Chaitanya Singh | Filed Under: C++ Programs

This program takes the string entered by user and finds the number of vowels and consonants in the input string.

Example: Program to find and display the number of vowels and Consonants in given String

In this program we are using char array to store the string entered by user and then in the for loop we are counting vowels and consonants. To understand this program you need to study the Arrays and for loop concepts of C++ Programming.

#include <iostream>
using namespace std;
int main(){
   char str[100];
   int vowelCounter = 0, consonantCounter = 0;
   cout << "Enter any string: ";
   cin.getline(str, 150);
   //'\0 represent end of string
   for(int i = 0; str[i]!='\0'; i++) {
      if(str[i]=='a' || str[i]=='e' || str[i]=='i' ||
         str[i]=='o' || str[i]=='u' || str[i]=='A' ||
         str[i]=='E' || str[i]=='I' || str[i]=='O' ||
         str[i]=='U')
      {
         vowelCounter++;
      }
      else if((str[i]>='a'&& str[i]<='z') || (str[i]>='A'&& str[i]<='Z'))
      {
         consonantCounter++;
      }
   }
   cout << "Vowels in String: " << vowelCounter << endl;
   cout << "Consonants in String: " << consonantCounter << endl;
   return 0;
}

Output:

Enter any string: Hello Guys, Welcome to Beginnersbook.com
Vowels in String: 13
Consonants in String: 21

The same program can be written by using Strings in C++. To use string in place of char array, replace the following part of the code in above program:

char str[100];
int vowelCounter = 0, consonantCounter = 0;
cout << "Enter any string: ";
cin.getline(str, 150);

with this code:

string str;
int vowelCounter = 0, consonantCounter = 0;

cout << "Enter any string: ";
getline(cin, str);

Top Related Articles:

  1. C++ Program to add two numbers
  2. C++ Program to Find and display the Transpose of a Matrix
  3. C++ Program to Add Complex Numbers
  4. C++ Program to Add two Matrices
  5. C++ Program for Binary Search

About the Author

I have 15 years of experience in the IT industry, working with renowned multinational corporations. Additionally, I have dedicated over a decade to teaching, allowing me to refine my skills in delivering information in a simple and easily understandable manner.

– Chaitanya

Leave a Reply Cancel reply

Your email address will not be published. Required fields are marked *

Copyright © 2012 – 2025 BeginnersBook . Privacy Policy . Sitemap