Alphabets

Program to count vowels and consonants in a string in C

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

 

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.