Skip to content
Menu
Code for Java c
  • Home
  • Java
    • Java Examples
    • Java tutorials
  • C
    • C tutorials
    • C Examples
  • C++
    • C++ Tutorials
    • C++ Examples
  • Python
    • Python Tutorials
    • Python Examples
  • About
    • About me
    • contact us
    • disclaimer
    • Privacy Policy
Code for Java c
C++ program to count number of vowels, consonants in a string

C++ program to count number of vowels, consonants in a string

Posted on April 24, 2022April 24, 2022

Table of Contents

  • C++ program to count number of vowels, consonants in a string
    • Code to count number Vowels and consonants  in given string
      • Code to count number of vowels and consonants using for loop
      • Code to count number of vowels and consonants using while loop
      • Code to count number of vowels and consonants using do-while loop
    • Related

C++ program to count number of vowels, consonants in a string

In this article, we will discuss the concept of C++ program to count number of vowels and consonants in a string

In this post, we are going to learn how to write a program to  count Total number of vowels and consonants in a string in C++ language

Code to count number Vowels and consonants  in given string

Code to count number of vowels and consonants using for loop

In this code, we are going to learn how to write a program to count total number of vowels and consonants present in the given string using if else statements in C++ language

Program 1

#include <iostream>
#include <conio.h>
#include <stdio.h>
using namespace std;

int main()
{
    char str[100];//Char array declaration
    int i;//declare variable i
    int vowelCount=0,consonantCount=0;
//declare and initialize counter variable
    cout<<"Please enter a string\n";//ask input from user
    gets(str);//reading the input
    for(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'){
    vowelCount++;//count vowels
  }
  else if((str[i] >= 'a' && str[i] <= 'z' || str[i] >= 'A' && str[i] <= 'Z' )){
    consonantCount++;//count consonant
  }
}
cout<<"The number of vowels: "<<vowelCount;
cout<<"\nThe number of consonant: "<<consonantCount;


getch();
    return 0;
}


When the above code is executed, it produces the following result

Please enter a string
Vowels
The number of vowels: 2
The number of consonants: 4

 

Code to count number of vowels and consonants using while loop

In this code, we are going to learn how to count total number of vowels and consonants present in the given string using while loop in C++ language

Program 2

#include <iostream>
#include <conio.h>
#include <stdio.h>
using namespace std;

int main()
{
    char str[100];//Char array declaration
    int i;//declare variable i
    int vowelCount=0,consonantCount=0;
//declare and initialize counter variable
    cout<<"Please enter a string\n";//ask input from user
    gets(str);//reading the input
    i=0;
    while(str[i]!='\0'){

  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'){
    vowelCount++;//count vowels
  }
  else if((str[i] >= 'a' && str[i] <= 'z' || str[i] >= 'A' && str[i] <= 'Z' )){
    consonantCount++;//count consonant
  }
   i++;
}
cout<<"The number of vowels: "<<vowelCount;
cout<<"\nThe number of consonant: "<<consonantCount;


getch();
    return 0;
}


When the above code is executed, it produces the following result

Please enter a string
Vowel count
The number of vowels: 4
The number of consonants: 6

 

Code to count number of vowels and consonants using do-while loop

In this code, we are going to learn how to count total number of vowels and consonants present in the given string using do-while loop in C++ language

Program 3

#include <iostream>
#include <conio.h>
#include <stdio.h>
using namespace std;

int main()
{
    char str[100];//Char array declaration
    int i;//declare variable i
    int vowelCount=0,consonantCount=0;
//declare and initialize counter variable
    cout<<"Please enter a string\n";//ask input from user
    gets(str);//reading the input
    i=0;
    do{

  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'){
    vowelCount++;//count vowels
  }
  else if((str[i] >= 'a' && str[i] <= 'z' || str[i] >= 'A' && str[i] <= 'Z' )){
    consonantCount++;//count consonant
  }
   i++;
}while(str[i]!='\0');
cout<<"The number of vowels: "<<vowelCount;
cout<<"\nThe number of consonant: "<<consonantCount;


getch();
    return 0;
}


When the above code is executed, it produces the following result

Please enter a string
Consonant count
The number of vowels: 5
The number of consonants: 9

 

Suggested post

If else statement in C++

Nested if statement in C++

Operator in C++ language

Switch statements in C++ language

 

Similar post

Java program to print all alphabets in given range

C program to print all alphabets in given range

C++ program to print all alphabets in given range

 

Java program to print all alphabets using loops

C program to print all alphabets using loops

C++ program to print all alphabets using loops

 

Java program to check whether a character is Alphabet or Not

C++ program to check whether a character is Alphabet or Not

C program to check whether a character is Alphabet or Not

Python program to check whether a character is Alphabet or Not

 

Related

Recent Posts

  • Multiply two numbers in Java using scanner| 5 different ways
  • 5 different ways to Divide two numbers in Java using scanner
  • Learn 8 Ways to Subtract Two Numbers Using Methods in Java
  • 10 ways to subtract two numbers in Java
  • Java Code Examples – Multiply Two Numbers in 5 Easy Ways
  • How to Divide two numbers in Java| 5 different ways

tag

Addition (8) Array (38) C++ language (91) C language (98) c sharp (23) Division (8) Function (29) if else (32) Java language (108) JavaScript (5) loops (138) Multiply (8) Oop (2) patterns (66) PHP (13) Python Language (38) Subtraction (9) temperature (20)

Archives

Categories

Address

Global information technology

Puloly south, PointPedro

Jaffna

Srilanka

©2026 Code for Java c | Powered by SuperbThemes