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
Note: It is advised to execute the program in Linux based system. Compile in linux using following code:
g++ -pthread program_name.cpp
Program:Â
CPP
// C++ program to find the frequency // of occurrences of a substring // in the given string using pthread Â
#include <iostream> #include <pthread.h> #include <stdlib.h> #include <sys/types.h> #include <time.h> #include <unistd.h> #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; } |
Java
// Java program for the above approachimport java.util.Arrays;Â
public class Main {Â Â Â Â static final int max = 4;Â Â Â Â static int[] count = new int[max];Â Â Â Â static String str, sub;Â
    public static void main(String[] args) {        int sum = 0;        final int[] x = new int[max];        for (int a = 0; a < max; a++)            x[a] = a;Â
        str = "prrrogramisprrrogramming";        sub = "rr";Â
        System.out.println("Enter the main string: " + str);        System.out.println("Enter the sequence to search: " + sub);Â
        Thread[] tid = new Thread[max];Â
        for (int i = 0; i < max; i++) {            final int value = i;            tid[i] = new Thread(() -> str_seq_count(x[value]));            tid[i].start();        }Â
        for (int i = 0; i < max; i++) {            try {                tid[i].join();            } catch (InterruptedException e) {                e.printStackTrace();            }        }Â
        for (int i = 0; i < max; i++)            sum = sum + count[i];Â
        System.out.println("Frequency of substring: " + sum);    }Â
    static void str_seq_count(int value) {        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.charAt(j) == str.charAt(k))                    k++;                else {                    flag = 1;                    break;                }            }            if (flag == 0)                count[value] += 1;        }    }}Â
// This code is contributed by adityashatmfh |
Python3
# Python program for the above approachÂ
import threadingÂ
count = [0] * 4str = "prrrogramisprrrogramming"sub = "rr"Â
def str_seq_count(value):    global count    l1 = len(str)    l2 = len(sub)    for i in range(value, l1, 4):        flag = 0        k = i        for j in range(l2):            if sub[j] == str[k]:                k += 1            else:                flag = 1                break        if flag == 0:            count[value] += 1Â
if __name__ == '__main__':Â Â Â Â total_count = 0Â Â Â Â x = [i for i in range(4)]Â Â Â Â print("Enter the main string: ", str)Â Â Â Â print("Enter the sequence to search: ", sub)Â
    threads = []    for i in range(4):        t = threading.Thread(target=str_seq_count, args=(x[i],))        threads.append(t)        t.start()    for t in threads:        t.join()Â
    total_count = sum(count)    print("Frequency of substring: ", total_count)Â
# This code is contributed by codebraxnzt |
C#
// C# program for the above approachÂ
using System;using System.Threading;Â
public class MainClass{Â Â Â Â static readonly int max = 4;Â Â Â Â static int[] count = new int[max];Â Â Â Â static string str, sub;Â
    public static void Main(string[] args)    {        int sum = 0;        int[] x = new int[max];        for (int a = 0; a < max; a++)            x[a] = a;Â
        str = "prrrogramisprrrogramming";        sub = "rr";Â
        Console.WriteLine("Enter the main string: " + str);        Console.WriteLine("Enter the sequence to search: " + sub);Â
        Thread[] tid = new Thread[max];Â
        for (int i = 0; i < max; i++)        {            int value = i;            tid[i] = new Thread(() => str_seq_count(x[value]));            tid[i].Start();        }Â
        for (int i = 0; i < max; i++)        {            try            {                tid[i].Join();            }            catch (ThreadInterruptedException e)            {                Console.WriteLine(e.StackTrace);            }        }Â
        for (int i = 0; i < max; i++)            sum = sum + count[i];Â
        Console.WriteLine("Frequency of substring: " + sum);    }Â
    static void str_seq_count(int value)    {        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;        }    }}Â
Â
// This code is contributed by Prince Kumar |
Javascript
// JavaScript program to find the frequency // of occurrences of a substring // in the given string using pthreadÂ
let count = [0, 0, 0, 0];let str = "prrrogramisprrrogramming";let sub = "rr";Â
function strSeqCount(value) {Â Â Â Â let l1 = str.length;Â Â Â Â let l2 = sub.length;Â Â Â Â for (let i = value; i < l1; i += 4) {Â Â Â Â Â Â Â Â let flag = 0;Â Â Â Â Â Â Â Â let k = i;Â Â Â Â Â Â Â Â for (let j = 0; j < l2; j++) {Â Â Â Â Â Â Â Â Â Â Â Â if (sub[j] === str[k]) {Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â k++;Â Â Â Â Â Â Â Â Â Â Â Â } else {Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â flag = 1;Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â break;Â Â Â Â Â Â Â Â Â Â Â Â }Â Â Â Â Â Â Â Â }Â Â Â Â Â Â Â Â if (flag === 0) {Â Â Â Â Â Â Â Â Â Â Â Â count[value]++;Â Â Â Â Â Â Â Â }Â Â Â Â }}Â
function runThreads() {Â Â Â Â let x = [0, 1, 2, 3];Â Â Â Â console.log("Enter the main string: ", str);Â Â Â Â console.log("Enter the sequence to search: ", sub);Â
    let threadIndex = 0;    function runNextThread() {        if (threadIndex < x.length) {            strSeqCount(x[threadIndex]);            threadIndex++;            setTimeout(runNextThread, 0); // Non-blocking I/O using setTimeout        } else {            finishExecution();        }    }Â
    runNextThread();}Â
function finishExecution() {Â Â Â Â let total_count = count.reduce((sum, value) => sum + value, 0);Â Â Â Â console.log("Frequency of substring: ", total_count);}Â
runThreads();Â
// This code is contributed by Samim Hossain Mondal |
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
Ready to dive in? Explore our Free Demo Content and join our DSA course, trusted by over 100,000 neveropen!
