Another recipe; ASCII TABLE codes

#include <stdio.h>
#include <stdlib.h>

#define MAX_CHARACTERS 128

void countCharacters(const char *text, int *charCount) {
    for (int i = 0; text[i] != '\0'; i++) {
        unsigned char currentChar = (unsigned char)text[i];
        if (currentChar >= 65 && currentChar <= 90) {
                    charCount[currentChar]++;
        } else         if (currentChar >= 97 && currentChar <= 122) {
                    charCount[currentChar-32]++;
        } else         if (currentChar >= 97 && currentChar <= 122) {
                    charCount[currentChar]++;
        }else         if (currentChar == 32 ) {
                    charCount[currentChar]++;
        }/*else  {
                    charCount[currentChar]++;
        }*/
    }
}

int main() {
    const char *text = "    As armas ___   e os barões assinalados na praia vitória! ";
    int charCount[MAX_CHARACTERS] = { 0 };

    countCharacters(text, charCount);

    printf("Character frequencies:\n");
    for (int i = 0; i < MAX_CHARACTERS; i++) {
        if (charCount[i] > 0) {
            printf("'%c' : %d\n", (char)i, charCount[i]);
        }
    }

    return 0;
}

In order to never miss it again…..

Leave a Comment

Your email address will not be published. Required fields are marked *