Alphabet

Program for count vowel and consonant of a string in C++ using ASCII value

Program for count vowel and consonant of a string in C++ using ASCII value

In this article, we will discuss the concept of Program to count vowel and consonant of a string in C++ using ASCII value

In this post, we are going to learn how to count the vowels and consonants in the given string using ASCII value in C++ programming language

count vowels and consonant

Code to count vowel and consonant of a string

Code to count the vowels and consonants using for loop

The program allows the user to enter a string  thereafter It counts the vowels and consonants of the given string using for loop in C++ language

Program 1

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

int main()
{
    char str[100];//character array declaration
    int i;
    int vowelCount=0,consonantCount=0;//counter variable declaration

    cout<<"Please enter a string\n";
    gets(str);//store the entered string
    for(i=0; str[i]!='\0'; i++){

    if(str[i] == 97|| str[i] == 101|| str[i] == 105|| str[i] == 111|| str[i] == 117
    ||str[i] == 65|| str[i] == 69|| str[i] == 73|| str[i] == 79|| str[i] == 85 ){
        vowelCount++;//vowel counter incremented by 1
    }
    else if((str[i] >= 97 && str[i] <= 122 || str[i] >= 65 && str[i] <= 90 )){
        consonantCount++;//consonant counter incremented by 1
    }
}
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

 

Approach

  1. Declare a character Array as char str[100];
  2. Declare and initialize two integer counter variable as int vowCount=0 and consCount=0;
  3. The user asked to enter a string to count vowels and consonants
  4. A for-loop is used to count total vowels and consonants of the given string
  5. It is initialized as i=0, and checks the test-expression and executes the loop until the given condition becomes true
  6. Use an if statement to test vowel, if the test expression is true,  vowCount becomes vowCount + 1(vowCount=vowCount+1)
  7. When the if-condition is false, control moves to else-if and checks the test expression of else-if
  8. If the test-expression of else if is true,  consCount becomes consCount + 1(consCount =consCount +1)
  9. If the test expression of else if is false, control skips from the loop
  10. Finally, the program displays the number of vowels and consonants of the given string.

Code to count the vowels and consonants using while loop

The program allows the user to enter a string  thereafter It counts the vowels and consonants of the given string using while loop in C++ language

Program 1

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

int main()
{
    char str[100];
    int i;
    int vowelCount=0,consonantCount=0;

    cout<<"Please enter a string\n";
    gets(str);
    i=0;
    while(str[i]!='\0'){

    if(str[i] == 97|| str[i] == 101|| str[i] == 105|| str[i] == 111|| str[i] == 117
    ||str[i] == 65|| str[i] == 69|| str[i] == 73|| str[i] == 79|| str[i] == 85 ){
        vowelCount++;
    }
    else if((str[i] >= 97 && str[i] <= 122 || str[i] >= 65 && str[i] <= 90 )){
        consonantCount++;
    }
     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

consonant count

The number of vowels:: 5

The number of consonants: 9

 

Approach

  1. Declare a character Array as char str[100];
  2. Declare and initialize two integer counter variable as int vowCount=0 and consCount=0;
  3. The user asked to enter a string to count vowels and consonants
  4. A while-loop is used to count total vowels and consonants of the given string
  5. It is initialized as i=0, and checks the test-expression and executes the loop until the given condition becomes true
  6. Use an if statement to test vowel, if the test expression is true, vowCount becomes vowCount + 1(vowCount=vowCount+1)
  7. When the if-condition is false, control moves to else if and checks the test expression of else-if
  8. If the test-expression of else-if is true, consCount becomes consCount + 1(consCount =consCount +1)
  9. i is incremented by 1
  10. If the test expression of else if is false, control skips from the loop
  11. Finally, the program displays the number of vowels and consonants of the given string.

 

Code to count the vowels and consonants using do-while loop

The program allows the user to enter a string  thereafter It counts the vowels and consonants of the given string using do-while loop 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;
    int vowelCount=0,consonantCount=0;
//counter variable declaration

    cout<<"Please enter a string\n";
    gets(str);//store the entered string from user
    i=0;
    do{

    if(str[i] == 97|| str[i] == 101|| str[i] == 105|| str[i] == 111|| str[i] == 117
    ||str[i] == 65|| str[i] == 69|| str[i] == 73|| str[i] == 79|| str[i] == 85 ){
        vowelCount++;//vowel counter incremented by 1
    }
    else if((str[i] >= 97 && str[i] <= 122 || str[i] >= 65 && str[i] <= 90 )){
        consonantCount++;//consonant counter incremented by 1
    }
     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
vowels and consonants
The number of vowels:: 5
The number of consonants: 9

 

Approach

  1. Declare a character Array as char str[100];
  2. Declare and initialize two integer counter variable as int vowCount=0 and consCount=0;
  3. The user asked to enter a string to count vowels and consonants
  4. A do-while-loop is used to count total vowels and consonants of the given string
  5. It is initialized as i=0, and checks the test-expression and executes the loop until the given condition becomes true
  6. Use an if statement to test vowel, if the test expression is true,  vowCount becomes vowCount + 1(vowCount=vowCount+1)
  7. When the if-condition is false, control moves to else if and checks the test expression of else-if
  8. If the test-expression of else-if is true,  consCount becomes consCount + 1(consCount =consCount +1)
  9. If the test expression of else if is false, control skips from the loop
  10. Finally, the program displays the number of vowels and consonants of the given string.

 

Suggested for you

for loop in C++ language

while loop in C++ language

The operator in C++ language

Data type in C++ language

Variable in C++ language

If-else statements in C++ Language

 

Similar post

C code to check whether the Alphabet is vowel or consonant

Python code to check whether the Alphabet is vowel or consonant

Java  code to check whether the Alphabet is vowel or consonant

C++  code to check whether the Alphabet is vowel or consonant

Java program to count vowels and consonants in a string

C program to count vowels and consonants in a string

Python program to count vowels and consonants in a string

Program to count vowel and consonant of a string in Java using ASCII value

Program to count vowel and consonant of a string inC++ using ASCII value

Program to count vowel and consonant of a string in C using ASCII value

 

 

Program to count vowel and consonant of a string in Java using ASCII value
Program for count vowels and consonants of a string in C using ASCII value
Karmehavannan

I am Mr S.Karmehavannan. Founder and CEO of this website. This website specially designed for the programming learners and very especially programming beginners, this website will gradually lead the learners to develop their programming skill.

Recent Posts

C# inverted full pyramid star pattern

C# inverted full pyramid star pattern In this article, we will discuss the concept of…

1 week ago

C# Full Pyramid star pattern program

C# Full Pyramid star pattern program In this article, we will discuss the concept of…

3 weeks ago

Program to count vowels,consonants,words, characters and space in Java

Program to count vowels, consonants, words, characters and space in Java In this article, we…

4 weeks ago

How to print multiplication table using Array in C++ language

How to print multiplication table using Array in C++ language In this post, we will…

4 weeks ago

C Program to multiplication table using Array

C Program to multiplication table using Array In this tutorial , we will discuss about…

1 month ago

Java program to check odd or even using recursion

Java program to check odd or even using recursion In this tutorial, we discuss a…

1 month ago