#include <stdio.h>
struct Character_Counts
{
int vowels;
int consonants;
int uppercase;
int lowercase;
int spaces;
int digits;
};
//add
int is_vowel(char ch) {
char lower_ch = (ch >= 'A' && ch <= 'Z') ? ch + 32 : ch; // Convert to lowercase if uppercase
return (lower_ch == 'a' || lower_ch == 'e' || lower_ch == 'i' || lower_ch == 'o' || lower_ch == 'u');
}
int is_alpha(char ch) {
return ((ch >= 'A' && ch <= 'Z') || (ch >= 'a' && ch <= 'z'));
}
int is_upper(char ch) {
return (ch >= 'A' && ch <= 'Z');
}
int is_lower(char ch) {
return (ch >= 'a' && ch <= 'z');
}
int is_digit(char ch) {
return (ch >= '0' && ch <= '9');
}
struct Character_Counts analyse_text(char text[])
{
struct Character_Counts counts = {0, 0, 0, 0, 0, 0};
char *ptr = text;
while (*ptr != '\0') {
char ch = *ptr;
if (is_alpha(ch)) {
if (is_upper(ch)) {
counts.uppercase++;
}
if (is_lower(ch)) {
counts.lowercase++;
}
if (is_vowel(ch)) {
counts.vowels++;
} else {
counts.consonants++;
}
} else if (is_digit(ch)) {
counts.digits++;
} else if (ch == ' ') {
counts.spaces++;
}
ptr++;
}
return counts;
}
void print_counts(struct Character_Counts data)
{
printf("Vowels = %d\n", data.vowels);
printf("Consonants = %d\n", data.consonants);
printf("Uppercase = %d\n", data.uppercase);
printf("Lowercase = %d\n", data.lowercase);
printf("Spaces = %d\n", data.spaces);
printf("Digits = %d\n", data.digits);
}
int main(void)
{
char buffer[80];
printf("> ");
scanf("%79[^\n]", buffer);
struct Character_Counts results = analyse_text(buffer);
print_counts(results);
return 0;
}
Preview:
downloadDownload PNG
downloadDownload JPEG
downloadDownload SVG
Tip: You can change the style, width & colours of the snippet with the inspect tool before clicking Download!
Click to optimize width for Twitter