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
#include <stdio.h>
#include <stdlib.h>
#include <stdbool.h>
#include <assert.h>
#include <string.h>

int count_ducks(char** string_array, int string_array_len) {
    char* search_string = "duck";
    int count = 0;
    for(int i = 0; i < string_array_len; i++) {
        if (strcmp(string_array[i], search_string) == 0) {
            count++;
        }
    }
    return count;
}

int main(int argc, char* argv[]) {
    char also_duck_str[] = "duck";
    char* not_duck_str = "not duck";
    printf("Trimmed %s\n", &not_duck_str[4]);
    char* strings[] = {
        "duck",
        "duck",
        "goose",
        also_duck_str,
        "not duck",
        "bird",
        not_duck_str + 4
    };
    int strings_len = sizeof(strings) / sizeof(*strings);
    int count = count_ducks(strings, strings_len);
    printf("Found %d ducks\n", count);
    return EXIT_SUCCESS;
}
/* vim: set tabstop=4 shiftwidth=4 fileencoding=utf-8 noexpandtab: */

© Copyright 2022 Alexander J. Quinn         This content is protected and may not be shared, uploaded, or distributed.