aboutsummaryrefslogtreecommitdiffstats
path: root/similarity.c
diff options
context:
space:
mode:
authorEliteTK <tomasz.kramkowski@gmail.com>2015-06-22 19:25:57 +0100
committerEliteTK <tomasz.kramkowski@gmail.com>2015-06-22 19:25:57 +0100
commitf942f2bcd4c8d0f90ff8dc18c89d9e8aa8b505ca (patch)
tree4a65e23e5052a6eece40768e9b332eb2f6c912c2 /similarity.c
parentda87fcf25e0c94e57f00df84679cd6fadc56ed46 (diff)
parentc38dd32029b7fdc7cb9d1cc4427a43d9d5fb7374 (diff)
downloadc-stuff-f942f2bcd4c8d0f90ff8dc18c89d9e8aa8b505ca.tar.gz
c-stuff-f942f2bcd4c8d0f90ff8dc18c89d9e8aa8b505ca.tar.xz
c-stuff-f942f2bcd4c8d0f90ff8dc18c89d9e8aa8b505ca.zip
Merge branch 'master' of https://github.com/EliteTK/c-stuff
Diffstat (limited to 'similarity.c')
-rw-r--r--similarity.c44
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;
+}