C Online Compiler
Example: Count Vowels Helper Function in C
C
C++
C#
Java
Python
PHP
main.c
STDIN
Run
// Count Vowels Helper Function #include <stdio.h> #include <string.h> // For strlen() #include <ctype.h> // For tolower() // Function to check if a character is a vowel (case-insensitive) int isVowel(char c) { char lower_c = tolower(c); // Convert to lowercase if (lower_c == 'a' || lower_c == 'e' || lower_c == 'i' || lower_c == 'o' || lower_c == 'u') { return 1; // It's a vowel } return 0; // It's not a vowel } int main() { // Step 1: Declare a character array (string) and a counter for vowels. char str[100]; int vowelCount = 0; int i; // Loop counter // Step 2: Prompt the user to enter a string. printf("Enter a string: "); // Step 3: Read the string from the user. fgets(str, sizeof(str), stdin); // Step 4: Remove the trailing newline character if it exists. str[strcspn(str, "\n")] = 0; // Step 5: Iterate through the string character by character. for (i = 0; str[i] != '\0'; i++) { // Step 6: Call the helper function to check if the current character is a vowel. if (isVowel(str[i])) { vowelCount++; } } // Step 7: Print the total count of vowels. printf("Number of vowels in the string: %d\n", vowelCount); return 0; }
Output
Clear
ADVERTISEMENTS