C Program to find union and intersection of two strings


In this program, You will learn how to find union and intersection of two strings in c.


1st is: a b c d e

2nd is: d e f

The union is: a b c d e f

The intersection is: d e

Example: How to find union and intersection of two strings in c.

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

void findUnion(char str1[], char str2[]) {

    int f, p = 0;
    int i, j;

    char str3[100];
    int len1 = strlen(str1);
    int len2 = strlen(str2);

    while (str1[p] != '\0') {
        str3[p] = str1[p];
        p++;
    }

    for (i = 0; i < len2; i++) {
        f = 0;
        for (j = 0; j < len1; j++) {
            if (str2[i] == str1[j]) {
                f = 1;
            }
        }
        if (f == 0) {
            str3[p] = str2[i];
            p++;
        }
    }
    str3[p] = '\0';
    printf("Union of two strings:%s", str3);
}

void findIntersection(char str1[], char str2[]) {

    int i, j, k = 0;
    char str3[100];

    int len1 = strlen(str1);
    int len2 = strlen(str2);

    for (i = 0; i < len1; i++) {
        for (j = 0; j < len2; j++) {
            if (str1[i] == str2[j]) {
                str3[k] = str1[i];
                k++;
            }
        }
    }
    str3[k] = '\0';
    printf("\nIntersection of two strings:%s", str3);
}

int main() {

    char str1[100];
    char str2[100];
    char str3[100];

    printf("Enter First String :");
    scanf("%[^\n]%*c", str1); 


    printf("Enter Second String :");
    scanf("%[^\n]%*c", str2); 


    findUnion(str1, str2);
    findIntersection(str1, str2);

    return 0;
}

Output:

Enter First String :abcd
Enter Second String :bcd
Union of two strings:abcd
Intersection of two strings:bcd