Ever thought about how to count vowels and consonants in a string using C? Let’s break it down. We’ll read a string from the user and tally up the vowels and consonants it has. For instance, if you put in "abcde", you'll find out there are 2 vowels and 3 consonants. Let’s dive into the details.
Prerequisites for the C Program to Count Vowels and Consonants
Before writing this program, ensure you have:
- Basic understanding of strings in C
- Familiarity with loops and conditional statements
- Knowledge of the
<stdio.h>and<string.h>header files - Any C compiler such as GCC 9 or newer — or just run it in the EduSeekho online compiler
Source Code of C Program to Count Vowels and Consonants in a String
Here's the complete code. It reads input, processes each character, and counts the vowels and consonants.
#include <stdio.h>
#include <string.h>
int main() {
char str[100];
int i, vowels = 0, consonants = 0;
// Input a string from the user
printf("Enter a string: ");
fgets(str, 100, stdin);
// Iterate through each character of the string
for(i = 0; i < strlen(str); i++) {
// Check if the character is a vowel
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') {
vowels++;
}
// Check if the character is a consonant
else if((str[i] >= 'a' && str[i] <= 'z') || (str[i] >= 'A' && str[i] <= 'Z')) {
consonants++;
}
}
// Print the count of vowels and consonants
printf("Vowels: %d\n", vowels);
printf("Consonants: %d\n", consonants);
return 0;
}Try this code yourself — free
Run it in the EduSeekho online compiler. No setup needed.
Output of the Count Vowels and Consonants Program
Once you run the program and enter a string like "abcde", you'll see the results.
Enter a string: abcde
Vowels: 2
Consonants: 3
Algorithm to Count Vowels and Consonants in C
Counting vowels and consonants involves checking each character and categorizing it. Here's how the program logically processes the string.
- Read the input string from the user.
- Initialize the vowel and consonant counters to zero.
- Iterate through each character of the string.
- Check if the character is a vowel. If yes, increment the vowel counter.
- If it's an alphabet but not a vowel, increment the consonant counter.
- Continue until all characters are processed.
- Output the counts of vowels and consonants.
flowchart TD
A([Start]) --> B[/Enter a string/]
B --> C[vowel = 0, consonant = 0]
C --> D{i < strlen(str)?}
D -->|Yes| E[Check if str[i] is a vowel]
E --> F{Is Vowel?}
F -->|Yes| G[vowel++]
F -->|No| H{Is Alphabet?}
H -->|Yes| I[consonant++]
H -->|No| J[i++]
G --> J
I --> J
J --> D
D -->|No| K([End])How the C Program to Count Vowels and Consonants Works
Let's break down the code and understand each part's function.
- The
#include <stdio.h>and#include <string.h>headers are necessary for input/output functions and string manipulation. - We declare a character array
strto store the user's input and two integer variablesvowelsandconsonantsto count each. fgets(str, 100, stdin)reads the input string including spaces.- The
forloop iterates over each character ofstrusingstrlen(str)to determine its length. - Inside the loop, an
ifstatement checks if a character is a vowel. If true, it incrementsvowels. - The
else ifchecks if a character is a consonant, incrementingconsonantsif true. - Finally,
printffunctions print the results.
Quick Quiz — test your understanding
Tap an option to check your answerWhat will be the output if the input string is 'abcde'?
Time and Space Complexity of the C Program
Analyzing the performance of our program is crucial.
Time Complexity: O(n) — n is the number of characters in the string, like 5 for "abcde". Each character is checked once, making the process linear. For instance, if you input a 10-character string, it makes 10 checks.
Space Complexity: O(1) — Only a few variables like vowels and consonants are used, regardless of the string length.
Common mistake: Not checking for both lowercase and uppercase vowels can mess up your counts.
Key takeaway: This program highlights character processing, a vital skill in text analysis.
FAQs About Counting Vowels and Consonants in C
Here are some common questions about this program:
fgets which reads spaces too.fgets is great for strings with spaces.if conditions to check for digits.Related Programs
If you found this program helpful, check out these related programs:
- C Program to Remove all Characters Except Alphabets
- C Program to Find the Frequency of Characters in a String
- C Program to Check Whether a Character is a Vowel or Consonant
- C Program to Check Whether a Character is an Alphabet
- C Program to Find ASCII Value of a Character
Conclusion
Counting vowels and consonants is a basic yet important task when dealing with text data. The logic here sets you up for more complex stuff like text analysis or natural language processing. If you found this interesting, try expanding the program to include other character types! Keep practicing to sharpen your skills.
Test your string manipulation skills with the C language quiz. Run the program in the EduSeekho compiler to see it work!