Introduction:
This C program counts the number of vowels and consonants in a given string. By reading user input and analyzing each character, the program distinguishes between vowels (a, e, i, o, u) and consonants (all other alphabetic characters). This functionality is useful in text analysis, language processing, and educational applications, helping users understand the composition of their text.
#include <stdio.h>
#include <ctype.h>
void countVowelsConsonants(const char *str, int *vowels, int *consonants) {
*vowels = 0;
*consonants = 0;
for (int i = 0; str[i] != '\0'; i++) {
char ch = tolower(str[i]);
if (isalpha(ch)) {
if (ch == 'a' || ch == 'e' || ch == 'i' || ch == 'o' || ch == 'u') {
(*vowels)++;
} else {
(*consonants)++;
}
}
}
}
int main() {
char str[100];
int vowels, consonants;
printf("Enter a string: ");
getchar(); // Consume newline character
fgets(str, sizeof(str), stdin);
str[strcspn(str, "\n")] = 0; // Remove newline character
countVowelsConsonants(str, &vowels, &consonants);
printf("Vowels: %d, Consonants: %d\n", vowels, consonants);
return 0;
}
Include Header Files:
#include <stdio.h>
: This header file is included for standard input and output functions likeprintf
andfgets
.#include <ctype.h>
: This header file is included for character handling functions liketolower
andisalpha
.
Function Declaration:
void countVowelsAndConsonants(const char *str, int *vowelCount, int *consonantCount)
: This function takes a string and two pointers to integers to store the count of vowels and consonants.
Initialize Counts:
*vowelCount = 0;
and*consonantCount = 0;
: Initializes the counts to zero at the beginning of the function.
Loop Through the String:
for (int i = 0; str[i] != '\0'; i++)
: A loop that iterates through each character of the string until it reaches the null terminator ('\0'
).
Character Conversion:
char ch = tolower(str[i]);
: Converts each character to lowercase to make the vowel check case-insensitive.
Check for Alphabet:
if (isalpha(ch))
: Checks if the character is an alphabet (i.e., A-Z or a-z).
Count Vowels and Consonants:
- If the character is a vowel (
'a', 'e', 'i', 'o', 'u'
), the vowel count is incremented. - If it is an alphabetic character but not a vowel, it is counted as a consonant.
Main Function:
- The program prompts the user to enter a string.
fgets
is used to read the string, allowing spaces to be included.- The newline character is removed using
str[strcspn(str, "\n")] = 0;
. - Finally, the
countVowelsAndConsonants
function is called, and the results are printed.
Input/Output Block:
Input:
- The user is prompted to enter a string.
Example Input:
Enter a string: Hello, World!
Output:
- The program outputs the count of vowels and consonants in the string.
Example Output:
Vowels: 3
Consonants: 7
Conclusion:
The vowel and consonant counting program effectively demonstrates how to analyze a string in C, showcasing basic string manipulation and conditional logic. By counting different types of characters, the program provides insights into the text’s composition, which can be valuable for educational purposes, linguistic studies, and data processing. Overall, this program serves as a practical example of character analysis in C, reinforcing fundamental programming concepts and techniques.