#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <ctype.h>
struct stringStats
{
int stringLength;
int upperCaseCount;
int lowerCaseCount;
int digitsCount;
int blankSpacesCount;
int nonAlphanumericCharsCount;
int vowelsCount; //lower or upper case, a,e,i,o,u
int nonVowelsCount; //lower or upper case
int specialCharactersCount;
char* nonAlphanumericChars; // not counting spaces
int xDigitCount; //number of hexadecimal "digits" - 0 through 9 & the letters A - F ... accept upper and lower case
int octalDigitsCount; //0-7
int binaryDigitsCount; //0 or 1
} ;
struct stringStats getStringStats (char theString[])
{
struct stringStats statsOfStr = {0,0,0,0,0,0,0,0,0,0,0,0,0};
statsOfStr.
stringLength = strlen(theString
); statsOfStr.
nonAlphanumericChars = malloc(statsOfStr.
stringLength * sizeof(char)); //have to free memory at end of program char * nonAlphanumericCharsPtr = statsOfStr.nonAlphanumericChars ; // current ptr
for (int i = 0; i < statsOfStr.stringLength; i++)
{
char currentChar = theString[i];
statsOfStr.upperCaseCount++;
}
statsOfStr.lowerCaseCount++;
}
statsOfStr.digitsCount++;
}
if (currentChar == ' ') {
statsOfStr.blankSpacesCount++;
}
statsOfStr.nonAlphanumericCharsCount++;
if (currentChar
!= ' ' && isprint(currentChar
) ) { *nonAlphanumericCharsPtr = currentChar;
nonAlphanumericCharsPtr++;
}
}
char lowerCurrentChar
= tolower(currentChar
); if (lowerCurrentChar == 'a' || lowerCurrentChar == 'e' || lowerCurrentChar == 'i' || lowerCurrentChar == 'o' || lowerCurrentChar == 'u') {
statsOfStr.vowelsCount++;
}
statsOfStr.nonVowelsCount++;
}
statsOfStr.specialCharactersCount++;
}
statsOfStr.xDigitCount++;
}
if (currentChar >= '0' && currentChar <= '7') {
statsOfStr.octalDigitsCount++;
}
if (currentChar == '0' || currentChar == '1') {
statsOfStr.binaryDigitsCount++;
}
}
*nonAlphanumericCharsPtr = 0; //null terminating this string
//free(statsOfStr.nonAlphanumericChars); should be called by the calling function when nonAlphanumericChars no longer needed
return statsOfStr;
}
int main(void) {
struct stringStats stats = getStringStats ("A5$0@");
printf("%d\n", stats.
stringLength); printf("%d\n", stats.
upperCaseCount); printf("%d\n", stats.
lowerCaseCount); printf("%d\n", stats.
digitsCount); printf("%d\n", stats.
blankSpacesCount); printf("%d\n", stats.
nonAlphanumericCharsCount); printf("%d\n", stats.
vowelsCount); printf("%d\n", stats.
nonVowelsCount); printf("%d\n", stats.
specialCharactersCount); printf("%s\n", stats.
nonAlphanumericChars); printf("%d\n", stats.
xDigitCount); printf("%d\n", stats.
octalDigitsCount); printf("%d\n", stats.
binaryDigitsCount); }
I2luY2x1ZGUgPHN0ZGlvLmg+CiNpbmNsdWRlIDxzdHJpbmcuaD4KI2luY2x1ZGUgPHN0ZGxpYi5oPgojaW5jbHVkZSA8Y3R5cGUuaD4KCnN0cnVjdCBzdHJpbmdTdGF0cwp7CiAgICBpbnQgc3RyaW5nTGVuZ3RoOwogICAgaW50IHVwcGVyQ2FzZUNvdW50OwogICAgaW50IGxvd2VyQ2FzZUNvdW50OwogICAgaW50IGRpZ2l0c0NvdW50OwogICAgaW50IGJsYW5rU3BhY2VzQ291bnQ7CiAgICBpbnQgbm9uQWxwaGFudW1lcmljQ2hhcnNDb3VudDsKICAgIGludCB2b3dlbHNDb3VudDsgLy9sb3dlciBvciB1cHBlciBjYXNlLCBhLGUsaSxvLHUKICAgIGludCBub25Wb3dlbHNDb3VudDsgLy9sb3dlciBvciB1cHBlciBjYXNlCiAgICBpbnQgc3BlY2lhbENoYXJhY3RlcnNDb3VudDsKICAgIGNoYXIqIG5vbkFscGhhbnVtZXJpY0NoYXJzOyAvLyBub3QgY291bnRpbmcgc3BhY2VzIAogICAgaW50IHhEaWdpdENvdW50OyAvL251bWJlciBvZiBoZXhhZGVjaW1hbCAiZGlnaXRzIiAtIDAgdGhyb3VnaCA5ICYgdGhlIGxldHRlcnMgQSAtIEYgLi4uIGFjY2VwdCB1cHBlciBhbmQgbG93ZXIgY2FzZQogICAgaW50IG9jdGFsRGlnaXRzQ291bnQ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AnKSB7CiAgICAgICAgICAgIHN0YXRzT2ZTdHIuYmxhbmtTcGFjZXNDb3VudCsrOwogICAgICAgIH0KICAgICAgICBpZiAoICFpc2FsbnVtKGN1cnJlbnRDaGFyKSApIHsKICAgICAgICAgICAgc3RhdHNPZlN0ci5ub25BbHBoYW51bWVyaWNDaGFyc0NvdW50Kys7CiAgICAgICAgICAgIGlmIChjdXJyZW50Q2hhciAhPSAnICcgJiYgaXNwcmludChjdXJyZW50Q2hhcikgKSB7CiAgICAgICAgICAgICAgICAqbm9uQWxwaGFudW1lcmljQ2hhcnNQdHIgPSBjdXJyZW50Q2hhcjsKICAgICAgICAgICAgICAgIG5vbkFscGhhbnVtZXJpY0NoYXJzUHRyKys7CiAgICAgICAgICAgIH0KICAgICAgICB9CiAgICAgICAgY2hhciBsb3dlckN1cnJlbnRDaGFyID0gdG9sb3dlcihjdXJyZW50Q2hhcik7CiAgICAgICAgaWYgKGxvd2VyQ3VycmVudENoYXIgPT0gJ2EnIHx8IGxvd2VyQ3VycmVudENoYXIgPT0gJ2UnIHx8IGxvd2VyQ3VycmVudENoYXIgPT0gJ2knIHx8IGxvd2VyQ3VycmVudENoYXIgPT0gJ28nIHx8IGxvd2VyQ3VycmVudENoYXIgPT0gJ3UnKSB7CiAgICAgICAgICAgIHN0YXRzT2ZTdHIudm93ZWxzQ291bnQrKzsKICAgICAgICB9CiAgICAgICAgZWxzZSBpZiAoaXNhbHBoYShjdXJyZW50Q2hhcikpIHsKICAgICAgICAgICAgc3RhdHNPZlN0ci5ub25Wb3dlbHNDb3VudCsrOwogICAgICAgIH0KICAgICAgICBpZiAoaXNwdW5jdChjdXJyZW50Q2hhcikpIHsKICAgICAgICAgICAgc3RhdHNPZlN0ci5zcGVjaWFsQ2hhcmFjdGVyc0NvdW50Kys7CiAgICAgICAgfQogICAgICAgIGlmIChpc3hkaWdpdChjdXJyZW50Q2hhcikpIHsKICAgICAgICAgICAgc3RhdHNPZlN0ci54RGlnaXRDb3VudCsrOwog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