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

Program to count vowels and consonants in a string in C

Posted on April 21, 2022April 21, 2022

Table of Contents

  • Program to count vowels and consonants in a string in C
    • Code to find total number of Vowels and consonants in a string
      • Code to count number of Vowels and consonants in a string, using for loop   – #1
      • Code to count number of Vowels and consonants in a string, using while loop   – #2
      • Code to count number of Vowels and consonants in a string, using do-while loop   – #3

Program to count vowels and consonants in a string in C

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

In this post, we are going to learn how to write a program to  count total number of vowels and consonants in given string input from user in C programming language

Code to find total number of Vowels and consonants in a string

Code to count number of Vowels and consonants in a string, using for loop   – #1

In this code, we are going to learn how to count  the total number of  Vowel and consonant in the given String using for loop in C language

Program 1

#include <stdio.h>
#include <stdlib.h>

int main()
{
    char str[100];//declare character array
    int i,vowCount=0,consCount=0;//Declare integer variables
    printf("Enter a string for count vowel and consonant\n");
    //Ask string input for count total number vowels and consonant
    gets(str);//reading the input string
    for(i=0; str[i]; 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' ){
    vowCount++;//count total number of vowels
  }
  else if((str[i] >= 'a' && str[i] <= 'z' || str[i] >= 'A' && str[i] <= 'Z' )){
    consCount++;//count total number of consonant
  }
}
printf("Number of vowels: %d \n",vowCount);//display total number of vowels
printf("Number of consonant: %d \n",consCount);//display total number of consonant
getch();
    return 0;
}

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

Enter a string for count vowel and consonant
Java code geeks
The number of vowels: 6
The number of consonant: 7

 

Code to count number of Vowels and consonants in a string, using while loop   – #2

In this code, we are going to learn how to count  the total number of  Vowel and consonant in the given String using while loop in C language

Program 2

#include <stdio.h>
#include <stdlib.h>

int main()
{
    char str[100];//declare array of characters
    int i,vowCount=0,consCount=0;//declare variables
    printf("Enter a string for count vowel and consonant\n");
    //Ask string input from user
    gets(str);//Reading string input from user
    i=0;
    while(str[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' ){
    vowCount++;//Count total number of vowels
  }
  else if((str[i] >= 'a' && str[i] <= 'z' || str[i] >= 'A' && str[i] <= 'Z' )){
    consCount++;//count total number of consonant
  }
   i++;
}
printf("Number of vowels: %d \n",vowCount);//display total number of vowels
printf("Number of consonant: %d \n",consCount);//display total number of consonant
getch();
    return 0;
}

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

Enter a string for count vowel and consonant
Java easy code
The number of vowels: 6
The number of consonant: 6

 

Code to count number of Vowels and consonants in a string, using do-while loop   – #3

In this code, we are going to learn how to count  the total number of  Vowel and consonant in the given String using do-while loop in C language

Program 3

#include <stdio.h>
#include <stdlib.h>

int main()
{
    char str[100];//Array declaration
    int i,vowCount=0,consCount=0;//Variable declaration and initializatuom
    printf("Enter a string for count vowel and consonant\n");
    //Ask string from the 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' ){
    vowCount++;//Count total number of vowels
  }
  else if((str[i] >= 'a' && str[i] <= 'z' || str[i] >= 'A' && str[i] <= 'Z' )){
    consCount++;//count total number of consonants
  }
   i++;
}while(str[i]);
printf("The number of vowels: %d \n",vowCount);//Display total number of vowels
printf("The number of consonant: %d \n",consCount);//Display total number of consonants
getch();
    return 0;
}

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

Enter a string for count vowel and consonant
my Java code
The number of vowels: 4
The number of consonant: 6

 

 

Suggested post

if else in C language

Nested if in C language

Operator in C language

Function 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

  • Subtract two numbers using method overriding
  • PHP Star triangle Pattern program
  • Using function or method to Write temperature conversion : Fahrenheit into Celsius
  • Function or method:temperature conversion from Fahrenheit into Celsius – Entered by user
  • Write temperature conversion program: Fahrenheit into Celsius
  • How to write a program to convert Fahrenheit into Celsius

tag

Addition (6) Array (38) C++ language (91) C language (98) c sharp (23) Division (6) Function (29) if else (32) Java language (102) JavaScript (5) loops (137) Multiply (7) Oop (2) patterns (65) PHP (13) Python Language (38) Subtraction (7) temperature (20)

Archives

Categories

Address

Global information technology

Puloly south, PointPedro

Jaffna

Srilanka

©2025 Code for Java c | Powered by SuperbThemes