Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <stdlib.h>
- #include <stdio.h>
- #include <math.h>
- int find_similar(char* str1, char* str2);
- int main(int argc, char** argv)
- {
- printf("%i\n", find_similar("FifthOfNovember", "September"));
- printf("%i\n", find_similar("Seventh", "September"));
- printf("%i\n", find_similar("JustForExample", "notEx"));
- }
- int find_similar(char* str1, char* str2)
- {
- size_t str1_len = strlen(str1);
- size_t str2_len = strlen(str2);
- if(str2_len > str1_len) { return -1; }
- int found = -1;
- for(int i = 0; i < str1_len; i++)
- {
- int similar = 0;
- found = -1;
- int* already_matched = calloc(1, str1_len);
- int matched_count = 0;
- for(int j = 0; j < str2_len; j++)
- {
- char c2 = str2[j];
- found = i;
- for(int k = i; k < str2_len + i; k++)
- {
- if(k >= str1_len) { break; }
- int skip_this_character = 0;
- for(int l = 0; l < matched_count; l++)
- {
- if(k == already_matched[l])
- {
- skip_this_character = true;
- break;
- }
- }
- if(skip_this_character) { continue; }
- char c1 = str1[k];
- if(c1 == c2)
- {
- already_matched[matched_count++] = k;
- similar++;
- break;
- }
- }
- }
- free(already_matched);
- if(similar > str2_len / 2)
- {
- break;
- }
- else
- {
- found = -1;
- }
- }
- return found;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement