Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Problem with union and intersection of two character strings. My Professor gave

ID: 668876 • Letter: P

Question

Problem with union and intersection of two character strings.

My Professor gave us this as homework (which i am having difficulty understanding)

My textbook has nothing on union or intersect.

Write a C++ program that determines the Union and Intersection of two character strings,
S1 and S2, and the results are placed in another string S3.

The program should have the following functions:

void Union (char X1[ ], char X2[ ], char X3[ ]);
void Intersect (char X1[ ], char X2[ ], char X3[ ]);

and in the main procedure S1, S2, and S3 are declared as arrays of characters.

int main ( )
{ char S1[100], S2 [100], S3[200];

We need the null character, because “when you write the Union and Intersect functions, the null character is used as a stop point, thus the logic of you program should use the null character”.

The easy way to enter the characters for S1 and S2 is by choosing a special character to stop entering them.

For example, let say we choose the character ‘*’ , as the stopping point.

This is what I have so far.

Explanation / Answer

============================================

Output

============================================

please enter the characters for S1(enter '*' when stop)abcd*

please enter the characters for S2(enter '*' when stop)cdefg*

S1: abcd

S2: abcd

Union:abcdefg

Intersect:cd

============================================

Program

============================================

#include <iostream>
#include <string>

using namespace std;

void Union(char X1[], char X2[], char X3[]) {
   int i = 0;
   int j = 0;
   bool found = false;

   while (X1[i] != '') {
       found = false;
       for (int k = 0; k < j; k++) {
           if (X3[k] == X1[i])
               found = true;
       }
       if (!found) {

           X3[j] = X1[i];
           j++;
       }
       i++;
   }

   i = 0;
   while (X2[i] != '') {
       found = false;
       for (int k = 0; k < j; k++) {
           if (X3[k] == X2[i])

               found = true;
       }
       if (!found) {

           X3[j] = X2[i];
           j++;
       }
       i++;
   }

   X3[j] = '';
}

void Intersect(char X1[], char X2[], char X3[]) {
   int i = 0;
   int j = 0, l;
   bool found = false;
   bool inbound = false;

   while (X1[i] != '') {
       l = 0;
       inbound = false;
       while (X2[l] != '') {
           if (X2[l] == X1[i])

               inbound = true;

           l++;
       }

       if (inbound == true) {
           found = false;
           for (int k = 0; k < j; k++) {
               if (X3[k] == X1[i])

                   found = true;
           }
           if (!found) {

               X3[j] = X1[i];
               j++;
           }
       }
       i++;
   }

   X3[j] = '';
}

int main() {
   char S1[100], S2[100], S3[200];

   char c; // string character
   cout << endl << "please enter the characters for S1(enter '*' when stop)";
   int i = 0;
   S1[0] = ' ';

   do {
       cin >> c;
       if (c != '*') {
           S1[i] = c;
           i++;
       } else {
           S1[i] = ''; //inserting the null character
       }
   } while (c != '*');

   cout << endl << "please enter the characters for S2(enter '*' when stop)";
   S2[0] = ' ';
   i = 0;
   do {
       cin >> c;
       if (c != '*') {
           S2[i] = c;
           i++;
       } else {
           S2[i] = ''; //inserting the null character
       }
   } while (c != '*');

   cout << "S1: " << S1 << endl;
   cout << "S2: " << S1 << endl;

   Union(S1, S2, S3);
   cout << "Union:" << S3 << endl;

   Intersect(S1, S2, S3);
   cout << "Intersect:" << S3 << endl;
   return 0;
}

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote