Skip to content

Instantly share code, notes, and snippets.

@swetland
Created September 12, 2019 08:15
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save swetland/550868bf21219d3942c0cdb0f19b5ab0 to your computer and use it in GitHub Desktop.
Save swetland/550868bf21219d3942c0cdb0f19b5ab0 to your computer and use it in GitHub Desktop.
count occurrences of a-z in short phrases
// letters.c - 2019 - brian swetland
#include <ctype.h>
#include <stdio.h>
#include <string.h>
int main(int argc, char** argv) {
char *x, line[256];
unsigned count[256];
unsigned extra, missing, n;
while ((x = fgets(line, sizeof(line), stdin)) != NULL) {
memset(count, 0, sizeof(count));
while (*x != 0) count[tolower(*x++)]++;
for (n = 'a'; n <= 'z'; n++) printf("%c", n);
printf(" %s", line);
for (extra = 0, missing = 0, n = 'a'; n <= 'z'; n++) {
if (count[n] == 0) missing++;
if (count[n] > 1) extra++;
printf("%u", count[n]);
}
printf(" -%u +%u\n", missing, extra);
}
return 0;
}
// abcdefghijklmnopqrstuvwxyz the quick brown fox jumps over the lazy dog
// 11113112111111411212211111 -0 +6
//
// abcdefghijklmnopqrstuvwxyz pack my box with five dozen liquor jugs
// 11112111311111311111211111 -0 +4
//
// abcdefghijklmnopqrstuvwxyz sphinx of black quartz, judge my vow
// 21111111111111211111211111 -0 +3
//
// abcdefghijklmnopqrstuvwxyz hello, world
// 00011001000300200100001000 -19 +2
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment