Table of Contents
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
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
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
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
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
Subtract two numbers using method overriding Program 1
PHP Star triangle Pattern program Here's a simple Java program that demonstrates how to print…
Using Function or Method to Write to temperature conversion: Fahrenheit into Celsius In this article,…
Function or method of temperature conversion from Fahrenheit into Celsius In this article, we will…
Write temperature conversion program: from Fahrenheit to Celsius In this article, we will discuss the…
How to write a program to convert Fahrenheit into Celsius In this article, we will…
This website uses cookies.