Program  to count the total number of vowels or consonants in a string.

Here, we have a basic program example to count vowels and consonants in a string using different languages. This program is created in c language, c++, Java, and Python.

Code to count vowels and consonants in C language

#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#define str_size 100
void main()
{
    char str[str_size];
    int i, len, vowel, cons;
    printf("Input the string : ");
    fgets(str, sizeof str, stdin);
    vowel = 0;
    cons = 0;
    len = strlen(str);
    for(i=0; i<len; 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')
        {
            vowel++;
        }
        else if((str[i]>='a' && str[i]<='z') || (str[i]>='A' && str[i]<='Z'))
        {
            cons++;
        }
    }
    printf("\nThe total number of vowel in the string is : %d\n", vowel);
    printf("The total number of consonant in the string is : %d\n\n", cons);
}

Code to count vowels and consonants in C language

#include <iostream>
using namespace std;
int main()
{
    char line[150];
    int vowels, cons;

    vowels = cons = 0;

    cout << "Enter a line of string: ";
    cin.getline(line, 150);
    for(int i = 0; line[i]!='\0'; ++i)
    {
        if(line[i]=='a' || line[i]=='e' || line[i]=='i' ||
           line[i]=='o' || line[i]=='u' || line[i]=='A' ||
           line[i]=='E' || line[i]=='I' || line[i]=='O' ||
           line[i]=='U')
        {
            ++vowels;
        }
        else if((line[i]>='a'&& line[i]<='z') || (line[i]>='A'&& line[i]<='Z'))
        {
            ++cons;
        }

    }

    cout << "The total number of vowel in the string is : " << vowels << endl;
    cout << "The total number of consonant in the string is : " << cons << endl;
    return 0;
}

Code to count vowels and consonants in C language

str = input("Please Enter Your Own String : ")
vowels = 0
cons = 0

for i in str:
    if(i == 'a' or i == 'e' or i == 'i' or i == 'o' or i == 'u'
       or i == 'A' or i == 'E' or i == 'I' or i == 'O' or i == 'U'):
        vowels = vowels + 1
    else:
        cons = cons + 1
 
print("Total Number of Vowels in this String = ", vowels)
print("Total Number of Consonants in this String = ", cons)

Code to count vowels and consonants in C language

import java.util.*;
public class countString
{
  public static void main(String[] args) {
  int vowel = 0, cons = 0;  
  Scanner in = new Scanner(System.in);
  System.out.print("Input the string: ");
  String str = in.nextLine();
  str = str.toLowerCase();  
   for(int i = 0; i < str.length(); i++)
    {  
       if(str.charAt(i) == 'a' || str.charAt(i) == 'e' || str.charAt(i) == 'i' || str.charAt(i) == 'o' ||  str.charAt(i) == 'u')
          {  
             vowel++;  
           }  
      else if(str.charAt(i) >= 'a' && str.charAt(i)<='z')
          {    
            cons++;  
          }  
    }  
System.out.println("Number of vowels in the given sentence is : " + vowel);  
System.out.println("Number of consonant in the given sentence is : " + cons);  
}
}