
- Learn C By Examples Time
- Learn C by Examples - Home
- C Examples - Simple Programs
- C Examples - Loops/Iterations
- C Examples - Patterns
- C Examples - Arrays
- C Examples - Strings
- C Examples - Mathematics
- C Examples - Linked List
- C Programming Useful Resources
- Learn C By Examples - Quick Guide
- Learn C By Examples - Resources
- Learn C By Examples - Discussion
String program to count vowels
Implementation
Now, we shall see the actual implementation of the program −
#include <stdio.h> int main() { char s[] = "TajMahal"; // String Given int i = 0; int vowels = 0; // Vowels counter int consonants = 0; // Consonants counter while(s[i++] != '\0') { if(s[i] == 'a' || s[i] == 'e' || s[i] == 'i' || s[i] == 'o' || s[i] == 'u' ) vowels++; else consonants++; } printf("'%s' contains %d vowels and %d consonants.", s, vowels, consonants); return 0; }
Output
Output of this program should be −
'TajMahal' contains 3 vowels and 5 consonants.
string_programs_in_c.htm
Advertisements