Frequency of a substring in a string using pthread

Last Updated : 13 Sep, 2023
Comments
Improve
Suggest changes
Like Article
Like
Report

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 approach
import 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] * 4
str = "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


Next Article
Practice Tags :

Similar Reads