// Assignment Question 5 Final Exam
//
// Name: <Tasos Paloukos>
//
// Class: C Programming, <Fall 2024>
//
// Date: <December 11, 2024>
#include <stdio.h>
#include <ctype.h>
// Define the structure
struct stringStats {
int stringLength;
int upperCaseCount;
int lowerCaseCount;
int digitCount;
int spaceCount;
int specialCharCount;
};
// Function prototype
struct stringStats getStringStats(char theString[]);
// Function definition
struct stringStats getStringStats(char theString[]) {
struct stringStats stats = {0, 0, 0, 0, 0, 0}; // Initialize all members to 0
int i = 0;
while (theString[i] != '\0') {
stats.stringLength++;
if (isupper(theString[i])) {
stats.upperCaseCount++;
} else if (islower(theString[i])) {
stats.lowerCaseCount++;
} else if (isdigit(theString[i])) {
stats.digitCount++;
} else if (isspace(theString[i])) {
stats.spaceCount++;
} else {
stats.specialCharCount++;
}
i++;
}
return stats;
}
// Main function
int main() {
char inputString[] = "Hello World! 123";
struct stringStats result = getStringStats(inputString);
printf("String Length: %d\n", result.stringLength);
printf("Uppercase Letters: %d\n", result.upperCaseCount);
printf("Lowercase Letters: %d\n", result.lowerCaseCount);
printf("Digits: %d\n", result.digitCount);
printf("Spaces: %d\n", result.spaceCount);
printf("Special Characters: %d\n", result.specialCharCount);
return 0;
}
Ly8gQXNzaWdubWVudCBRdWVzdGlvbiA1IEZpbmFsIEV4YW0KLy8KLy8gTmFtZTogPFRhc29zIFBhbG91a29zPgovLwovLyBDbGFzczogQyBQcm9ncmFtbWluZywgPEZhbGwgMjAyND4KLy8KLy8gRGF0ZTogPERlY2VtYmVyIDExLCAyMDI0PgoKI2luY2x1ZGUgPHN0ZGlvLmg+CiNpbmNsdWRlIDxjdHlwZS5oPgoKLy8gRGVmaW5lIHRoZSBzdHJ1Y3R1cmUKc3RydWN0IHN0cmluZ1N0YXRzIHsKICAgIGludCBzdHJpbmdMZW5ndGg7CiAgICBpbnQgdXBwZXJDYXNlQ291bnQ7CiAgICBpbnQgbG93ZXJDYXNlQ291bnQ7CiAgICBpbnQgZGlnaXRDb3VudDsKICAgIGludCBz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