-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcount_characters_main.c
More file actions
59 lines (54 loc) · 1.53 KB
/
count_characters_main.c
File metadata and controls
59 lines (54 loc) · 1.53 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
/*
6 kyu
Count characters in your string
https://www.codewars.com/kata/52efefcbcdf57161d4000091
*/
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define COUNTS_SIZE 128
void count(const char* string, unsigned counts[COUNTS_SIZE]);
static void do_test(const char* string, const unsigned expected[COUNTS_SIZE]) {
unsigned submitted[COUNTS_SIZE];
for (size_t index = 0; index < COUNTS_SIZE; index++) {
submitted[index] = rand() % 10;
}
count(string, submitted);
char sub_str[22 * COUNTS_SIZE + 1];
char exp_str[22 * COUNTS_SIZE + 1];
size_t index = 0;
size_t position = sprintf(sub_str, "{");
while (index < COUNTS_SIZE) {
position += sprintf(sub_str + position, "%u, ", submitted[index]);
index += 1;
}
sprintf(sub_str + position - 2, "}");
index = 0;
position = sprintf(exp_str, "{");
while (index < COUNTS_SIZE) {
position += sprintf(exp_str + position, "%u, ", expected[index]);
index += 1;
}
sprintf(exp_str + position - 2, "}");
printf("String = \"%s\"\nSubmitted: %s\nExpected: %s\n%s\n\n", string,
sub_str, exp_str, strcmp(sub_str, exp_str) == 0 ? "OK" : "FAIL");
}
int main(void) {
{
const unsigned expected[COUNTS_SIZE] = {['a'] = 2, ['b'] = 1};
do_test("aba", expected);
}
{
const unsigned expected[COUNTS_SIZE] = {0};
do_test("", expected);
}
{
const unsigned expected[COUNTS_SIZE] = {['a'] = 2};
do_test("aa", expected);
}
{
const unsigned expected[COUNTS_SIZE] = {['a'] = 2, ['b'] = 2};
do_test("aabb", expected);
}
return 0;
}