Frequency of a substring in a string using pthread

Given an input string and a substring. Find the frequency of occurrences of a substring in the given string using pthreads.

Examples:

```Input: string = "man"
substring = "dhimanman"
Output: 2

Input: string = "banana"
substring = "nn"
Output: 0
```

Recommended: Please try your approach on {IDE} first, before moving on to the solution.

Note: It is advised to execute the program in Linux based system.
Compile in linux using following code:

`g++ -pthread program_name.cpp`

Program:

 `// C++ program to find the frequency ` `// of occurrences of a substring ` `// in the given string using pthread ` ` `  `#include ` `#include ` `#include ` `#include ` `#include ` `#include ` `#define max 4 ` `using` `namespace` `std; ` ` `  `int` `count[max] = { 0 }; ` `string str, sub; ` ` `  `void``* str_seq_count(``void``* args) ` `{ ` `    ``int` `value = *(``int``*)args; ` `    ``int` `i, j, k, l1, l2, flag; ` ` `  `    ``// calculating length of string 1 ` `    ``l1 = str.length(); ` ` `  `    ``// calculating length of substring ` `    ``l2 = sub.length(); ` ` `  `    ``for` `(i = 0 + value; i < l1; i = i + max) { ` ` `  `        ``flag = 0; ` `        ``k = i; ` ` `  `        ``for` `(j = 0; j < l2; j++) { ` ` `  `            ``// flag=0; ` `            ``if` `(sub[j] == str[k]) ` `                ``k++; ` `            ``else` `{ ` `                ``flag = 1; ` `                ``break``; ` `            ``} ` `        ``} ` `        ``if` `(flag == 0) ` `            ``count[value] += 1; ` `    ``} ` `} ` ` `  `// Driver code ` `int` `main() ` `{ ` `    ``int` `sum = 0; ` `    ``int` `x[max]; ` `    ``for` `(``int` `a = 0; a < max; a++) ` `        ``x[a] = a; ` ` `  `    ``str = ``"prrrogramisprrrogramming"``; ` `    ``sub = ``"rr"``; ` ` `  `    ``cout << ``"Enter the main string: "` `         ``<< str << endl; ` `    ``cout << ``"Enter the sequence to search: "` `         ``<< sub << endl; ` ` `  `    ``int` `i, l1; ` ` `  `    ``pthread_t tid[max]; ` ` `  `    ``for` `(i = 0; i < max; i++) { ` `        ``pthread_create(&tid[i], NULL, ` `                       ``str_seq_count, ` `                       ``(``void``*)&x[i]); ` `    ``} ` `    ``for` `(i = 0; i < max; i++) ` `        ``pthread_join(tid[i], NULL); ` `    ``for` `(i = 0; i < max; i++) ` `        ``sum = sum + count[i]; ` `    ``cout << ``"Frequency of substring: "` `         ``<< sum; ` ` `  `    ``return` `0; ` `} `

Output:

```Enter the main string: prrrogramisprrrogramming
Enter the sequence to search: rr
Frequency of substring: 4
```

Related article: Frequency of a substring in a string

My Personal Notes arrow_drop_up

Check out this Author's contributed articles.

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.

Article Tags :
Practice Tags :

Be the First to upvote.

Please write to us at contribute@geeksforgeeks.org to report any issue with the above content.