-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsimilarity.c
44 lines (36 loc) · 880 Bytes
/
similarity.c
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
#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;
}