I'm trying to compare the number of similar characters between 2 string and came across strpbrk() function. But I can't find any method to split my search string into array of character.
char search[] = "chzi";
char arr[2][20] = {"cheang", "wai"};
float lengthSearch = strlen(search);
float count = 0;
for(int i = 0; i < 2; i++){
int lengthArr = strlen(arr[i]);
for(int j = 0; j < lengthSearch; j++){
if(strpbrk(&search[j], arr[i])){
printf("%c is the similarity between %s and %s\n", *strpbrk(&search[j], arr[i]), &search[j], arr[i]);
count++;
printf("count is now %.1f\n", count);
}
}
float probability = ((count/lengthSearch) * (count/lengthArr)) * 100;
printf("%s has a probability of %.2f\n\n", arr[i], probability);
count = 0;
}
The problem is here
i is the similarity between chzi and wai
count is now 1.0
i is the similarity between hzi and wai
count is now 2.0
i is the similarity between zi and wai
count is now 3.0
i is the similarity between i and wai
count is now 4.0
instead of chzi I only want to compare c and wai
searchstring is already an array of characters and you can iterate over each character with a simplefor (int i = 0; search[i]; i++)wheresearch[i]will be each character insearchin sequence.if(strpbrk(&search[j], arr[i]))is backwards. If you want to know if any of the chars insearchappear inarr[i]thenstrpbrk (arr[i], search)floatis wrong,size_tit the return type forstrlen. Seeman 3 strlenstrpbrk(&seach[j], arr[i]), if i am able to just writestrpbrk(search[j], arr[i])I believe this code should be goodstrpbrk(&search[j], arr[i])