0

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

10
  • Your search string is already an array of characters and you can iterate over each character with a simple for (int i = 0; search[i]; i++) where search[i] will be each character in search in sequence. if(strpbrk(&search[j], arr[i])) is backwards. If you want to know if any of the chars in search appear in arr[i] then strpbrk (arr[i], search) Commented Nov 17, 2019 at 5:55
  • float is wrong, size_t it the return type for strlen. See man 3 strlen Commented Nov 17, 2019 at 6:02
  • looping through the string will return me each character but when using strpbrk(), it forces me to write in strpbrk(&seach[j], arr[i]), if i am able to just write strpbrk(search[j], arr[i]) I believe this code should be good Commented Nov 17, 2019 at 6:03
  • strpbrk doesn't work the way you think it works, read the documentation carefully. Commented Nov 17, 2019 at 6:06
  • I want to check if the first character of search matches any character in the string, that's why I wrote strpbrk(&search[j], arr[i]) Commented Nov 17, 2019 at 6:11

1 Answer 1

2

I wanted to check how many similar characters between search and arr[i]

Then you can use strpbrk but somewhat reversed from how you attempted. man 3 strpbrk with declaration of

char *strpbrk(const char *s, const char *accept);

locates the first occurrence in the string s
of any of the bytes in the string accept.

So what you want do is simply loop with strpbrk and find out how many characters of search are common to arr[i]. Using a pointer makes that simple, e.g.

#include <stdio.h>
#include <string.h>

int main (void) {

    char search[] = "chzi",
        arr[][20] = {"cheang", "wai"};
    size_t n = sizeof arr / sizeof *arr;

    for (size_t i = 0; i < n; i++) {
        size_t count = 0;
        char *p = arr[i];
        while ((p = strpbrk (p, search)))
            count++, p++;
        printf ("%s contains %zu occurrence of characters in %s.\n",
                arr[i], count, search);
    }
}

Note above you simply use a pointer to arr[i] and then use strpbrk to locate the first character in search that occurs in arr[i]. You then increment the pointer to the next character in arr[i] (using p++;) and do it again until strpbrk returns NULL indicating there are no more character is arr[i] that match any of the characters in search, the code above does just this, e.g.

        char *p = arr[i];
        while ((p = strpbrk (p, search)))
            count++, p++;

which if you wanted to avoid using the comma operator would be:

        char *p = arr[i];
        while ((p = strpbrk (p, search))) {
            count++;
            p++;
        }

Example Use/Output

Running with your strings would result in:

$ ./bin/strpbrkchars
cheang contains 2 occurrence of characters in chzi.
wai contains 1 occurrence of characters in chzi.

Look things over and let me know if that is what you intended. You will need to add your probability code, but that is left to you.

Sign up to request clarification or add additional context in comments.

3 Comments

What does this do? p = strpbrk (p, search) Does it means that if the first character of p matches any character in search it will return that character and store it in p?
Recall, strpbrk returns a pointer to the first character in p (arr[i]) that matches any of the characters in search. So in the case of cheang, strpbrk will return a pointer to c at the beginning. You then increment count++ and p++ so p now points to h and you loop again. h matches so strpbrk returns a pointer to h on the second iteration. You then count++; and p++; so p now points to e. On your next call, nothing in "eang" matches any character in "chzi", so strpbrk returns NULL completing the loop.
Add a new string "caching" to your array and you will find count == 4 as it matches c (2-times), h (1-time) and i (1-time).

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.