Compute character frequency in a string
clib:
$ clib install jwerle/chfreq.c
source:
$ git clone git@github.com:jwerle/chfreq.c.git
$ cd chfreq.c
$ make
$ make install
#include <stdlib.h>
#include <stdint.h>
#include <stdio.h>
#include "chfreq.h"
int
main (void) {
char *str = "aabbcc";
uint32_t freq** = chfreq(str);
printf("%c %d\n", freq[0][0], freq[0][1]);
printf("%c %d\n", freq[1][0], freq[1][1]);
printf("%c %d\n", freq[2][0], freq[2][1]);
return 0;
}
...yields:
a 2
b 2
c 2
With the command line utility:
$ echo -n aabbcc | chfreq
a | 2
b | 2
c | 2
$ echo -n 'kinkajous are awesome !'| chfreq
k | 2
i | 1
n | 1
a | 3
j | 1
o | 2
u | 1
s | 2
| 3
r | 1
e | 3
w | 1
m | 1
! | 1
$ echo -n werle | chfreq -f '%c(%d)'
w(1)
e(2)
r(1)
l(1)
uint32_t **
chfreq (const char *src);
Returns an array of `uint32_t' type arrays who's character is the first index and the occurence is the second index.
example structure:
uint32_t mat[2][2] = {
{'a', 2},
NULL
}
MIT