i am writing a program which will find the matching pairs between two strings.Then i am finding the sum of each pair & find the minimum.I want to only those pairs which is traverse only once.I am facing problem at the last stage i.e. to delete the pairs which are already traversed. plz help me.I am implemented in c. plz help... here is the code.... Code: #include<stdio.h> #include<conio.h> #include<string.h> int sum(int,int); int pairs(int,int); int main() { char s1[50],s2[50]; int i,j,l1,l2,k,m,x[50],p[50],q[50],b[50],c[50],r,a=0,count=0,min,l; printf("enter the two strings\n"); gets(s1); gets(s2); l1=strlen(s1); l2=strlen(s2); printf("length1=%d length2=%d \n",l1,l2); for(i=0;i<l1;i++) { for(j=0;j<l2;j++) { if(s1[i]==s2[j]) { k=i; m=j; p[a]=k; q[a]=m; x[a]=sum(k,m); printf("p[%d]=( %d,%d ) sum=%d\n",count+1,k,m,x[a]); //printf("%d %d",p[a],q[a]); a++; count++; } } } //printf("matching pairs=%d %d",p[0],q[0]); for(i=0;i<count;i++) { for(j=i+1;j<count;j++) { if(x[i]>x[j]) { min=x[i]; x[i]=x[j]; x[j]=min; } } for(r=0;r<count;r++) { if((p[r]+q[r])==x[i]) { b[i]=p[r]; c[i]=q[r]; printf("minimum %d %d=%d \n",b[i],c[i],x[i]); } /*if((l==x[i])>1) { //b[i]=p[r]; //c[i]=q[r]; if((p[r]>b[i-1])&&(q[r]>c[i-1])) { b[i]=p[r]; c[i]=q[r]; printf("minimum %d %d=%d \n",b[i],c[i],x[i]); } } */ } /*if((b[i+1]<=b[i])&&(c[i+1]<=c[i])) { printf("\nremove pairs %d %d\n",b[i+1],c[i+1]); } */ } //if((p[i]+q[i]==x[i])) //{ //printf("\nmatching pairs %d %d",p[i],q[i]); //} // } getch(); return 0; } int sum(int k,int m) { int s; s=k+m; return s; }
maybe an array to keep track of pairs? Code: unsigned char pairs[52] = { 0 }; // assuming alpha only if(s1[i] == s2[i]) { if(isupper(s1[i]) && pairs[s1[i] - 'A'] == 0) // ucase match // count sum; set pairs[s1[i] - 'A'] to 1 else if(pairs[s1[i] - 'a'] == 0) // lcase match // count sum; set pairs[s1[i] - 'a'] to 1 ... other bits of code } I'm not sure if it's what you're after, but maybe it will help to spark an idea