i have to make a program to find the intersection of two strings, i have made program but its not working.
any help will be appreciated.:)

#include <cstdlib>
#include <iostream>


using namespace std;

void intersection(char *str1, char *str2)
{
     int len1,len2,i=0,j,flag=0;
     char temp[30];
     
     while(*str1!='\0')         //outer loop for 1st string.
     {
      while(*str2!='\0')        //inner loop to compare each letter of str2 with 1,2,3 and so on char of str1.
      {
      if(*str1==*str2)          //if both char are equal
      {
       for(j=0;j<i;j++)
       {
          if((temp[j]==*str1)&&(i>0)) //check if its duplicate or unique before inserting in a temporary array.
          {
            flag=0;
            break;
          }
          else
          flag=1;
       }
       
      if(flag==1)
       {
       temp[i]=(*str1);
       i++;
       }
      }
      str2++;
      }
      str1++;
     }
     
     cout<<endl<<temp;
     
 }
int main(int argc, char *argv[])
{
    char str1[20];
	char str2[20];
	
    cout<<"enter the 1st string"<<endl;
    gets(str1);
	cout<<"enter the 2nd string"<<endl;
	gets(str2);
	
	intersection(str1,str2);
	
    system("PAUSE");
    return EXIT_SUCCESS;
}

Recommended Answers

All 7 Replies

>i have to make a program to find the intersection of two strings, i have made program but its not working.
any help will be appreciated.

Could you please give me an example on what you mean with: to find the intersection of two strings?
I don't get what you mean by that.
And when I understand the task, I can move on to check the code :)

I too don't exactly see what you're trying to do, however you are using temp before you set it!

It's almost like you're trying to create a new list containing characters that occur in both strings.

But your algorithm is flawed if so!

intersection of 2 strings. e.g
string1 contains: abcdefgerbcad
string2 contains: qwbebczdelkn
then the intersection of above 2 strings should be: bcde
i.e elements common to both sets and it shud appear in the result only once,no matter how many times common elements are there independently in each string or say a set.

i hope you get it, if not then goggle intersection and unions.

I'm pretty sure he means the mathematical intersection of two sets?

IE:
str1: abcdefghi
str2:ghiablkj
the intersection would be a,b,g,h,i
so, all letters that are in both strings.

Now, problems:
1.) You try to send the temp array to cout, but you never put a null character in it, so it'll print well past the end of your array
2.)you're incrementing the actual str2 , so by the time you finish with your internal while loop the first time, it'll never go through it again. Does this make sense?

I'm pretty sure he means the mathematical intersection of two sets?

IE:
str1: abcdefghi
str2:ghiablkj
the intersection would be a,b,g,h,i
so, all letters that are in both strings.

Now, problems:
1.) You try to send the temp array to cout, but you never put a null character in it, so it'll print well past the end of your array
2.)you're incrementing the actual str2 , so by the time you finish with your internal while loop the first time, it'll never go through it again. Does this make sense?

oohhh yeh....you go it right.
thanks for the help, i ll try it again.
thanks

I get it.
Are you allowed to use string functions of the standard function library?
In that case you could simplify the whole thing :)

BTW, does it have to be case sensitive?
I mean should inputting the following two strings:

String 1: hi
String 2: Hey

Result in the following intersection: h ?

I guess this is homework, but if it is not, the library already has this stuff:

#include <algorithm>
#include <iostream>
#include <iterator>
#include <set>
#include <string>

using namespace std;

// use basic_string<T> to allow any char type
template <class T>
basic_string<T> Intersection(
                             const basic_string<T>& a, 
                             const basic_string<T>& b)
{
    set<T> sa(a.begin(), a.end());
    set<T> sb(b.begin(), b.end());
    basic_string<T> c;

    set_intersection(
        sa.begin(), sa.end(), 
        sb.begin(), sb.end(), 
        back_inserter(c));

    return c;
}

int main()
{
    string a = "abcdefgerbcad";
    string b = "qwbebczdelkn";

    cout << Intersection(a, b) << '\n';
}

If it is homework, I am not allowed to give you any code, but there is a simple algorithm with two histogram arrays. The first array is a histogram of the characters in the first string out of all characters. The second array is a histogram of the second string. Loop over both arrays and print every character that has a value greater than 0 to find the intersection.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.