Skip to content

Instantly share code, notes, and snippets.

@untodesu
Created May 20, 2022 14:50
Show Gist options
  • Save untodesu/4686ff3be00c9db13d1fca5d6c839186 to your computer and use it in GitHub Desktop.
Save untodesu/4686ff3be00c9db13d1fca5d6c839186 to your computer and use it in GitHub Desktop.
#include <ctype.h>
#include <stdio.h>
#include <string.h>
/* counts case-insensitive entries for ch */
static int numchars(const char *s, int ch)
{
int r, cv;
const char *sv;
r = 0;
cv = tolower(ch);
sv = s;
while(*sv && (sv = strchr(sv, cv))) {
r++;
sv++;
}
cv = toupper(ch);
sv = s;
while(*sv && (sv = strchr(sv, cv))) {
r++;
sv++;
}
return r;
}
int main(void)
{
char line[512] = { 0 };
int na, nb, nc;
na = 0;
nb = 0;
nc = 0;
while(fgets(line, sizeof(line), stdin)) {
na += numchars(line, 'a');
nb += numchars(line, 'b');
nc += numchars(line, 'c');
}
printf("na = %d\n", na);
printf("nb = %d\n", nb);
printf("nc = %d\n", nc);
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment