C program to count vowels and consonants in a string
C program to count vowels and consonants in a string
In this article, we will discuss the concept of C program to count vowels and consonants in a string
In this post, we are going to learn how to count the vowels and consonants in the given string in C programming language
Code to count the vowels and consonants using for loop
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];//declara a char array int i,vowCount=0,consCount=0;//declare counter variables printf("Enter a string for count vowel and consonant\n"); gets(str); 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++; } else if((str[i] >= 'a' && str[i] <= 'z' || str[i] >= 'A' && str[i] <= 'Z' )){ consCount++; } } printf("The number of vowels: %d \n",vowCount); printf("The number of consonant: %d \n",consCount); getch(); return 0; }
When the above code is executed, it produces the following result
Enter a string for count vowel and consonant Vowels The number of vowels: 2 The number of vowels: 4
Approach
- Declare a string using character Array as char str[100];
- Declare and initialize two integer counter variable as int vowCount=0 and consCount=0;
- The user asked to enter a string to count vowels and consonants
- A for-loop is used to count total vowels and consonants of the given string
- It is initialized as i=0, and checks the test-expression and executes the loop until the given condition becomes true
- Use an if statement to test vowel, if the test expression is true, vowCount becomes vowCount + 1(vowCount=vowCount+1)
- When the if-condition is false, control moves to else if and checks the test expression of else-if
- If the test-expression of else-if is true, consCount becomes consCount + 1(consCount =consCount +1)
- If the test expression of else-if is false, control skips from the loop
- Finally, the program displays the number of vowels and consonants of the given string.
C program to count vowel and consonant using while loop
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];//declare a character array int i,vowCount=0,consCount=0;//declare counter variables printf("Enter a string for count vowel and consonant\n"); gets(str);//store the string input from the 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++; } else if((str[i] >= 'a' && str[i] <= 'z' || str[i] >= 'A' && str[i] <= 'Z' )){ consCount++; } i++; } printf("The number of vowels: %d \n",vowCount); printf("The number of consonant: %d \n",consCount); getch(); return 0; }
When the above code is executed, it produces the following result
Enter a string for count vowel and consonant Vowel and consonant The number of vowels: 6 The number of vowels: 11
Approach
- Declare a character Array as char str[100];
- Declare and initialize two integer counter variable as int vowCount=0 and consCount=0;
- The user asked to enter a string to count vowels and consonants
- A while-loop is used to count total vowels and consonants of the given string
- It is initialized as i=0, and checks the test expression and executes the loop until the given condition becomes true
- Use an if statement to test vowel, if the test expression is true, vowCount becomes vowCount + 1(vowCount=vowCount+1)
- When the if-condition is false, control moves to else-if and checks the test expression of else-if
- If the test-expression of else-if is true, consCount becomes consCount + 1(consCount =consCount +1)
- If the test expression of else-if is false, control skips from the loop
- Finally, the program displays the number of vowels and consonants of the given string.
C program to count vowel and consonant using do-while loop
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,vowCount=0,consCount=0; printf("Enter a string for count vowel and consonant\n"); gets(str); 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++; } else if((str[i] >= 'a' && str[i] <= 'z' || str[i] >= 'A' && str[i] <= 'Z' )){ consCount++; } i++; }while(str[i]); printf("The number of vowels: %d \n",vowCount); printf("The number of consonant: %d \n",consCount); getch(); return 0; }
When the above code is executed, it produces the following result
Enter a string for count vowel and consonant Alphabet letters The number of vowels: 5 The number of vowels: 10
Approach
- Declare a character Array as char str[100];
- Declare and initialize two integer counter variable as int vowCount=0 and consCount=0;
- The user asked to enter a string to count vowels and consonants
- A do-while loop is used to count total vowels and consonants of the given string
- It is initialized as i=0, and checks the test-expression and executes the loop until the given condition becomes true
- Use an if statement to test vowel, if the test expression is true, vowCount becomes vowCount + 1(vowCount=vowCount+1)
- When the if-statement is false, control moves to else-if and checks the test expression of else-if
- If the test-expression of else-if is true, consCount becomes consCount + 1(consCount =consCount +1)
- If the test expression of else-if is false, control skips from the loop
- Finally, the program displays the number of vowels and consonants of the given string.
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