diff options
author | Tomasz Kramkowski <tk@the-tk.com> | 2015-06-21 14:31:04 +0100 |
---|---|---|
committer | Tomasz Kramkowski <tk@the-tk.com> | 2015-06-21 14:31:04 +0100 |
commit | c38dd32029b7fdc7cb9d1cc4427a43d9d5fb7374 (patch) | |
tree | ca671e8e8f8dd4645792e099432734cfa37a27a4 | |
parent | a72bf3a44e8002181b2818ab557434a2a0a22c96 (diff) | |
download | c-stuff-c38dd32029b7fdc7cb9d1cc4427a43d9d5fb7374.tar.gz c-stuff-c38dd32029b7fdc7cb9d1cc4427a43d9d5fb7374.tar.xz c-stuff-c38dd32029b7fdc7cb9d1cc4427a43d9d5fb7374.zip |
similarity.c
-rw-r--r-- | similarity.c | 44 |
1 files changed, 44 insertions, 0 deletions
diff --git a/similarity.c b/similarity.c new file mode 100644 index 0000000..b1fbd06 --- /dev/null +++ b/similarity.c @@ -0,0 +1,44 @@ +#include <errno.h> +#include <error.h> +#include <stdbool.h> +#include <stdio.h> +#include <string.h> + +int main(int argc, char **argv) +{ + unsigned long matches = 0, misses = 0; + char *strings[2]; + size_t lengths[2]; + size_t pos[2] = {0}; + unsigned current = 0; + + if (argc != 3) + error(1, 0, "Incorrect number of arguments %d", argc); + + strings[0] = argv[1]; + lengths[0] = strlen(strings[0]); + strings[1] = argv[2]; + lengths[1] = strlen(strings[1]); + + for (; pos[0] < lengths[0] && pos[1] < lengths[1];) { + if (strings[0][pos[0]] == strings[1][pos[1]]) { + matches++; + pos[0]++; + pos[1]++; + current = current ? 0 : 1; + } else { + pos[current ? 0 : 1]++; + misses++; + } + } + + if (lengths[0] > pos[0]) + misses += lengths[0] - pos[0] - 1; + + if (lengths[1] > pos[1]) + misses += lengths[1] - pos[1] - 1; + + printf("Matches: %lu, Misses: %lu\n", matches, misses); + + return 0; +} |