I a currently trying to make a program in C to return the number of similar characters between two strings, but without counting a letter multiple times.
Checking if two strings are exactly the same too.
For example with "week" and "weak", it should return 3 because the letters 'w', 'e', and 'k' are in both words.
The size of the two strings are 4 only.
I made the "contains" function to check if we didn't already compared a character, for example with "test" and "tast" got 2 "t", so it was returning more than expected.
For now my code, seems to work but not correctly and I can't understand why...
#include <stdio.h>
#include <string.h>
#include <stdbool.h>
#define STRLEN 4
bool contains(char tab[], char c){
for(int i=0; i<STRLEN; i++){
if(tab[i] == c){
return 1;
}
}
return 0;
}
int likeness(char* password, char* wordchosen) {
if(strlen(password)!=STRLEN && strlen(wordchosen)!=STRLEN) {
return -1;
}
if(!strcmp(password, wordchosen)) {
return 4;
}
int like = 0;
char found[STRLEN];
for(int i=0; i<STRLEN; i++) {
for(int j=0; j<STRLEN; j++) {
/*printf("%c : %c\n", password[i], wordchosen[j]);
printf("\n");*/
if(password[i] == wordchosen[j] && !(contains(found, wordchosen[j]))) {
like++;
}
}
found[i] = password[i];
}
return like;
}
int main() {
printf("week and week = %d\n", likeness("week", "week")); // should be 4 (same word even if only 3 (w, e, k))
printf("test and tast = %d\n", likeness("test", "tast")); // should be 2 (t, s)
printf("week and weak = %d\n", likeness("week", "weak")); // should be 3 (w, e, k)
printf("snet and sent = %d\n", likeness("snet", "sent")); // should be 4 (s, n, e, t)
printf("gree and green = %d\n", likeness("gree", "gren")); // should be 3 (g, r, e)
printf("mail and main = %d\n", likeness("mail", "main")); // should be 3 (m, a, i)
printf("same = %d\n", likeness("same", "same")); // should be 4
// opposite side in the likeness func. return different result...
return 0;
}