C Online Compiler
Example: Count Vowels Direct Iteration in C
C
C++
C#
Java
Python
PHP
main.c
STDIN
Run
// Count Vowels Direct Iteration #include <stdio.h> #include <string.h> // For strlen() #include <ctype.h> // For tolower() 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. // Using fgets to safely read a line of input including spaces. // It includes the newline character, so we might need to handle it. fgets(str, sizeof(str), stdin); // Step 4: Remove the trailing newline character if it exists. // strlen(str) returns length including newline if present. str[strcspn(str, "\n")] = 0; // Step 5: Iterate through the string character by character. for (i = 0; str[i] != '\0'; i++) { // Step 6: Convert the current character to lowercase for case-insensitive comparison. char ch = tolower(str[i]); // Step 7: Check if the lowercase character is a vowel. if (ch == 'a' || ch == 'e' || ch == 'i' || ch == 'o' || ch == 'u') { vowelCount++; } } // Step 8: Print the total count of vowels. printf("Number of vowels in the string: %d\n", vowelCount); return 0; }
Output
Clear
ADVERTISEMENTS