I have a function counter(string s1, string s2) which takes two strings (both size 4) of numbers as its parameters. s1 is randomly generated while s2 is user-input.
int counter(string s1, string s2){
int count = 0;
int i, j;
if(i != j){
for(i = 0; i < 4; i++){
for(j = 0; j < 4; j++){
if(s2[i] != s2[j] && s2[j] == s1[i]){
count += 1;
}
}
}
}
return count;
}
What this function does is compare each element of s2 to s1's. If an element of s2 is equal to an element of s1, provided that they do not have the same index or position, count increments by 1. The function, however, encounters problems when s2 contains duplicates. For example, if s1 = 1234 and s2 = 4445, the output should be 1, but my program outputs 3. How should I be able to detect the duplicates in the string?
Aucun commentaire:
Enregistrer un commentaire