C Online Compiler
Example: Count Vowels Consonants Digits in C
C
C++
C#
Java
Python
PHP
main.c
STDIN
Run
// Count Vowels Consonants Digits #include <stdio.h> #include <string.h> // For strlen() #include <ctype.h> // For tolower() and isalpha(), isdigit() int main() { char str[100]; int vowels = 0; int consonants = 0; int digits = 0; int i; // Step 1: Get input string from the user printf("Enter a string: "); fgets(str, sizeof(str), stdin); // Reads string including spaces // Remove the newline character if present from fgets str[strcspn(str, "\n")] = 0; // Step 2: Iterate through each character of the string for (i = 0; str[i] != '\0'; i++) { char ch = str[i]; // Step 3: Check if the character is a digit if (ch >= '0' && ch <= '9') { digits++; } // Step 4: Check if the character is an alphabet else if ((ch >= 'a' && ch <= 'z') || (ch >= 'A' && ch <= 'Z')) { // Convert to lowercase for consistent vowel checking ch = tolower(ch); // Step 5: Check if the lowercase alphabet is a vowel if (ch == 'a' || ch == 'e' || ch == 'i' || ch == 'o' || ch == 'u') { vowels++; } // Step 6: Otherwise, it's a consonant else { consonants++; } } // Characters that are not digits or alphabets (e.g., spaces, punctuation) are ignored. } // Step 7: Print the final counts printf("Number of Vowels: %d\n", vowels); printf("Number of Consonants: %d\n", consonants); printf("Number of Digits: %d\n", digits); return 0; }
Output
Clear
ADVERTISEMENTS