In this article, we will discuss the concept of Program to count vowels and consonants of a string in C using ASCII value
In this post, we are going to learn how to count the vowels and consonants in the given string using ASCII value in C programming language
The program allows the user to enter a string thereafter It counts the vowels and consonants of the given string using for loop in C language
Program 1
#include <stdio.h> #include <stdlib.h> int main() { char str[100]; int i; int vowelCount=0,consonantCount=0; printf("Please enter a string\n"); gets(str); for(i=0; str[i]!='\0'; i++){ if(str[i] == 97|| str[i] == 101|| str[i] == 105|| str[i] == 111|| str[i] == 117 ||str[i] == 65|| str[i] == 69|| str[i] == 73|| str[i] == 79|| str[i] == 85 ){ vowelCount++; } else if((str[i] >= 97 && str[i] <= 122 || str[i] >= 65 && str[i] <= 90 )){ consonantCount++; } } printf("The number of vowels: %d",vowelCount); printf("\nThe number of consonants: %d",consonantCount); getch(); return 0; }
When the above code is executed, it produces the following result
Please enter a string C program The number of vowels:2 The number of consonants: 6
Approach
The program allows the user to enter a string thereafter It counts the vowels and consonants of the given string using while loop in C language
Program 2
#include <stdio.h> #include <stdlib.h> int main() { char str[100]; int i; int vowelCount=0,consonantCount=0; printf("Please enter a string\n"); gets(str); i=0; while(str[i]!='\0'){ if(str[i] == 97|| str[i] == 101|| str[i] == 105|| str[i] == 111|| str[i] == 117 ||str[i] == 65|| str[i] == 69|| str[i] == 73|| str[i] == 79|| str[i] == 85 ){ vowelCount++; } else if((str[i] >= 97 && str[i] <= 122 || str[i] >= 65 && str[i] <= 90 )){ consonantCount++; } i++; } printf("The number of vowels: %d",vowelCount); printf("\nThe number of consonant: %d",consonantCount); getch(); return 0; }
When the above code is executed, it produces the following result
Please enter a string code for coding The number of vowels: 5 The number of consonants: 8
Approach
The program allows the user to enter a string thereafter It counts the vowels and consonants of the given string using do-while loop in C language
Program 3
#include <stdio.h> #include <stdlib.h> int main() { char str[100]; int i; int vowelCount=0,consonantCount=0; printf("Please enter a string\n"); gets(str); i=0; do{ if(str[i] == 97|| str[i] == 101|| str[i] == 105|| str[i] == 111|| str[i] == 117 ||str[i] == 65|| str[i] == 69|| str[i] == 73|| str[i] == 79|| str[i] == 85 ){ vowelCount++; } else if((str[i] >= 97 && str[i] <= 122 || str[i] >= 65 && str[i] <= 90 )){ consonantCount++; } i++; } while(str[i]!='\0'); printf("The number of vowels: %d",vowelCount); printf("\nThe number of consonant: %d",consonantCount); getch(); return 0; }
Please enter a string
C programming language The number of vowels: 7 The number of consonants: 13
Approach
Suggested for you
If-else statements in C Language
Similar post
C code to check whether the Alphabet is vowel or consonant
Python code to check whether the Alphabet is vowel or consonant
Java code to check whether the Alphabet is vowel or consonant
C++ code to check whether the Alphabet is vowel or consonant
Java program to count vowels and consonants in a string
C++ program to count vowels and consonants in a string
Python program to count vowels and consonants in a string
Program to count vowel and consonant of a string in Java using ASCII value
Program to count vowel and consonant of a string inC++ using ASCII value
C# inverted full pyramid star pattern In this article, we will discuss the concept of…
C# Full Pyramid star pattern program In this article, we will discuss the concept of…
Program to count vowels, consonants, words, characters and space in Java In this article, we…
How to print multiplication table using Array in C++ language In this post, we will…
C Program to multiplication table using Array In this tutorial , we will discuss about…
Java program to check odd or even using recursion In this tutorial, we discuss a…