solution_6_3_vowel_counter.c
C Programming Language/solutions/intermediate/week6/solution_6_3_vowel_counter.c
/**
* Solution 6.3: Vowel Counter
* Week 6 - Arrays and String Basics
*
* Description: Count vowels in a string
*/
#include <stdio.h>
#include <string.h>
#include <ctype.h>
int countVowels(char str[]) {
int count = 0;
int len = strlen(str);
for (int i = 0; i < len; i++) {
char ch = tolower(str[i]);
if (ch == 'a' || ch == 'e' || ch == 'i' || ch == 'o' || ch == 'u') {
count++;
}
}
return count;
}
int main() {
char str[100];
printf("Enter a string: ");
fgets(str, sizeof(str), stdin);
// Remove newline character
str[strcspn(str, "\n")] = '\0';
int vowels = countVowels(str);
printf("Number of vowels: %d\n", vowels);
char *newstr;
/* str = "Xyz abc Mnd abc QPR abc" */
newstr = str;
do {
printf("New string: %s\n", newstr); /* print the new string */
newstr = strstr(str, "abc");
newstr = strstr(newstr + 3, "abc");
newstr = strstr(newstr + 3, "abc");
} while (newstr != NULL);
return 0;
}
相關文章
bitwise_operations_advanced.c
bitwise_operations_advanced.c — c source code from the C Programming Language learning materials (C Programming Language/additional/advanced_topics/bitwise_operations_advanced.c).
閱讀文章 →huffman_coding_complete.c
huffman_coding_complete.c — c source code from the C Programming Language learning materials (C Programming Language/additional/advanced_topics/huffman_coding_complete.c).
閱讀文章 →recursion_advanced.c
recursion_advanced.c — c source code from the C Programming Language learning materials (C Programming Language/additional/algorithms/recursion_advanced.c).
閱讀文章 →sorting_algorithms_complete.c
sorting_algorithms_complete.c — c source code from the C Programming Language learning materials (C Programming Language/additional/algorithms/sorting_algorithms_complete.c).
閱讀文章 →binary_trees_complete.c
binary_trees_complete.c — c source code from the C Programming Language learning materials (C Programming Language/additional/data_structures/binary_trees_complete.c).
閱讀文章 →linked_lists_complete.c
linked_lists_complete.c — c source code from the C Programming Language learning materials (C Programming Language/additional/data_structures/linked_lists_complete.c).
閱讀文章 →