Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
26 changes: 26 additions & 0 deletions count_vowels.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
#include <stdio.h>

// Program to count vowels in a string
int main() {
char str[100];
int i, count = 0;

// Ask the user to enter a string
printf("Enter a string: ");
fgets(str, sizeof(str), stdin); // safer input method than gets()

// Loop through the string
for (i = 0; str[i] != '\0'; i++) {
// Check for both uppercase and lowercase vowels
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') {
count++;
}
}

// Display the total number of vowels
printf("Number of vowels: %d\n", count);

return 0;
}