From c38dd32029b7fdc7cb9d1cc4427a43d9d5fb7374 Mon Sep 17 00:00:00 2001 From: Tomasz Kramkowski Date: Sun, 21 Jun 2015 14:31:04 +0100 Subject: similarity.c --- similarity.c | 44 ++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 44 insertions(+) create mode 100644 similarity.c 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 +#include +#include +#include +#include + +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; +} -- cgit v1.2.3-54-g00ecf