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;
}