Cheang Wai Bin
Cheang Wai Bin

Reputation: 191

How to split a string and store each character into an array

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

Upvotes: 0

Views: 190

Answers (1)

David C. Rankin
David C. Rankin

Reputation: 84521

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.

Upvotes: 2

Related Questions