Pointer Vowel

PHOTO EMBED

Tue Jun 11 2024 20:43:03 GMT+0000 (Coordinated Universal Time)

Saved by @meanaspotato #c #function

#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;
}
content_copyCOPY

The objective of this exercise is to analyze a given string and count various types of characters in it. Specifically, you are required to count the number of vowels, consonants, uppercase characters, lowercase characters, spaces, and digits in a given C-string. Requirements Data Structures The Character_Counts struct is defined with fields to hold the counts for vowels, consonants, uppercase and lowercase characters, spaces, and digits. Function Declarations and Definitions Implement analyse_text that takes in a character array (C-string) and returns a Character_Counts struct populated with the counts. The print_counts function is already implemented and takes a Character_Counts struct as an argument to print out the various counts. Main Function In the main() function, a buffer array is defined, and user input is read into this buffer. The analyse_text function is then called with this buffer as an argument, and its return value is stored in a Character_Counts struct. This struct is then passed to print_counts to display the counts.