Alphabets

Program to count number of vowels, consonants, spaces in a sentence in C++

Program to count number of vowels, consonants and spaces in a sentence in C++

In this article, we will discuss the concept of Program to count number of vowels, consonants an spaces in a sentence in C++

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

Code to count number Vowels, consonants and spaces in given sentence

Code to count total number of vowels, consonants and spaces using for loop

In this code, we are going to learn how to count total number of vowels, consonants and spaces in the given sentence 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, spaceCount=0;
//declare and initialize counter variable
    cout<<"Please enter a sentence\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
  }
else if(str[i] == ' '){
    spaceCount++;//count spaces
  }
}
cout<<"The number of vowels: "<<vowelCount;
cout<<"\nThe number of consonants: "<<consonantCount;
cout<<"\nThe number of spaces: "<<spaceCount;



getch();
    return 0;
}


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

Please enter a sentence
Count vowels and consonants
The number of vowels:8
The number of consonants: 16
The number of spaces: 3

 

Code to count total number of vowels, consonants and spaces using while loop

In this code, we are going to learn how to count total number of vowels, consonants and spaces in the given sentence 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, spaceCount=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
  }
else if(str[i] == ' '){
    spaceCount++;//count spaces
  }
   i++;
}
cout<<"The number of vowels: "<<vowelCount;
cout<<"\nThe number of consonants: "<<consonantCount;
cout<<"\nThe number of spaces: "<<spaceCount;



getch();
    return 0;
}



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

Please enter a sentence
Vowels and consonants with spaces
The number of vowels:9
The number of consonants: 20
The number of spaces: 4

 

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

In this code, we are going to learn how to count total number of vowels, consonants and spaces in the given sentence using 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, spaceCount=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
  }
else if(str[i] == ' '){
    spaceCount++;//count spaces
  }
   i++;
}while(str[i]!='\0');
cout<<"The number of vowels: "<<vowelCount;
cout<<"\nThe number of consonants: "<<consonantCount;
cout<<"\nThe number of spaces: "<<spaceCount;



getch();
    return 0;
}




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

Please enter a sentence
Counting Vowels and consonants without spaces
The number of vowels: 14
The number of consonants: 26
The number of spaces: 5

 

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

 

Karmehavannan

Recent Posts

Subtract two numbers using method overriding

Subtract two numbers using method overriding   Program 1

3 months ago

PHP Star triangle Pattern program

PHP Star triangle Pattern program Here's a simple Java program that demonstrates how to print…

3 months ago

Using function or method to Write temperature conversion : Fahrenheit into Celsius

Using Function or Method to Write to temperature conversion: Fahrenheit into Celsius In this article,…

1 year ago

Function or method:temperature conversion from Fahrenheit into Celsius – Entered by user

Function or method of temperature conversion from Fahrenheit into Celsius In this article, we will…

1 year ago

Write temperature conversion program: Fahrenheit into Celsius

Write temperature conversion program: from Fahrenheit to Celsius In this article, we will discuss the…

1 year ago

How to write a program to convert Fahrenheit into Celsius

How to write a program to convert Fahrenheit into Celsius In this article, we will…

1 year ago

This website uses cookies.