C Online Compiler
Example: Count Vowels and Consonants in C
C
C++
C#
Java
Python
PHP
main.c
STDIN
Run
// Count Vowels and Consonants #include <stdio.h> #include <string.h> // For strlen() #include <ctype.h> // For isalpha() and tolower() int main() { // Step 1: Declare and initialize a string and counters char str[100]; int vowels = 0; int consonants = 0; int i; // Step 2: Prompt user for input printf("Enter a string: "); fgets(str, sizeof(str), stdin); // Read string including spaces // Remove trailing newline character if present from fgets str[strcspn(str, "\n")] = 0; // Step 3: Iterate through the string for (i = 0; str[i] != '\0'; i++) { // Step 4: Check if the character is an alphabet if (isalpha(str[i])) { // Step 5: Convert to lowercase for case-insensitive check char ch = tolower(str[i]); // Step 6: Check if it's a vowel if (ch == 'a' || ch == 'e' || ch == 'i' || ch == 'o' || ch == 'u') { vowels++; } else { // Step 7: If not a vowel, it's a consonant consonants++; } } } // Step 8: Print the results printf("Number of vowels: %d\n", vowels); printf("Number of consonants: %d\n", consonants); return 0; }
Output
Clear
ADVERTISEMENTS