Code:#include<stdio.h> #include<string.h> #include<stdlib.h> int ulen,slen,mergelen; char **final; int overlap_last(char *st) { int i,k,j,length; char last; int overlap_at; overlap_at=0; length=strlen(st); last=st[length-1]; for(k=0;k<=((length-3)/2);k++) { if(st[length-1-k-1]==last && st[length-1-k-1-k-1]==last) { overlap_at=length-1-k-1; for(j=0;j<k;j++) { if(st[length-1-k-1-k+j]!=st[length-1-k+j]) { overlap_at=0; break; } } if(overlap_at!=0) break; } } return overlap_at; } void overlapfree_merge_sets(char **u, int ulen, char **s, int slen) { int i,j,k,overlap,len,l,p,duplicate,finallen,length; char *temp; char *temp1; overlap=0; duplicate=0; final=(char **)malloc(sizeof(char *)*ulen); for(i=0;i<ulen;i++) { length=strlen(u[i]); final[i]=(char *)malloc(length*sizeof(char)); strcpy(final[i],u[i]); } finallen=ulen; for(i=0;i<ulen;i++) { for(j=0;j<slen;j++) { length=strlen(u[i]); temp=(char *)malloc(length*sizeof(char)); strcpy(temp,u[i]); len=strlen(temp); strcat(temp,s[j]); overlap=0; for(k=len+1;k<=strlen(temp);k++) { temp1=(char *)malloc(sizeof(char)*k); strncpy(temp1,temp,k); if(overlap_last(temp1)) { overlap=1; free(temp1); break; } free(temp1); } //adding overlap free concatenated element of u[i] and s[j] if(overlap!=1 && k==(strlen(temp)+1)) { duplicate=0; for(l=0;l<finallen;l++) { if(strcmp(final[l],temp)==0) { duplicate=1; break; } else duplicate=0; } if(duplicate!=1) { length=strlen(temp); final[finallen]=(char *)malloc(length*sizeof(char)); strcpy(final[finallen],temp); finallen++; } } free(temp); ///adding all element of s if(i==0) { duplicate=0; for(l=0;l<finallen;l++) { if(strcmp(final[l],s[j])==0) { duplicate=1; break; } else duplicate=0; } if(duplicate!=1) { length=strlen(s[j]); final[finallen]=(char *)malloc(length*sizeof(char)); strcpy(final[finallen],s[j]); finallen++; } } } //s loop }///u loop mergelen=finallen; } int main() { int i,k; char **u; char **s; char **t; ulen=2; slen=2; u=(char **)malloc(sizeof(char *)*ulen); for(i=0;i<ulen;i++) { printf("k="); scanf("%d",&k); u[i]=(char *)malloc(sizeof(char)*k); printf("u[%d]=",i); scanf("%s",u[i]); } printf("\n"); s=(char **)malloc(sizeof(char *)*slen); for(i=0;i<slen;i++) { printf("k="); scanf("%d",&k); s[i]=(char *)malloc(sizeof(char)*k); printf("s[%d]=",i); scanf("%s",s[i]); } overlapfree_merge_sets(u,ulen,s,slen); for(i=0;i<mergelen;i++) { printf("%s\n",final[i]); } printf("\n"); return 0; } When you run above programme enter value like below: k=1 u[0]=a k=1 u[1]=b k=1 s[0]=a k=1 s[1]=b output: �A� it should be a b aa ab ba bb Logic of programme: Suppose, we have given two sets of strings, e.g. u={a,b} and s={a,b}. These set might be different. Generate all possible concatenation of element of u followed by element of s (u and s contains epsilon as a element), e.g. output final={a,b,aa,ab,ba,bb} for above example. Out of those concatenation only some are acceptable in the final set. Means, concatenated element should satisfy some specific criteria.That criteria we are checking in overlap_last function. Another example: u={a,b,aa,bb} s={a} final={a,b,aa,ba,bba} aaa is not satisfying our criteria. Overlap last function is working correct. My problemi: output is stored in global variable final. Except final[0] all final[i] are correct. It is working fine according to my logic. But, some where memory problem is occurring. I had debug this programme. Every thing is working correct untill value of finallen=3, correct final[0]=a. After finallen=4 and when programme execute this below line it assigns final[0]=�A�. final[finallen]=(char *)malloc(length*sizeof(char)); if i increase set size then it gives some more values like above, then other correct values. But, it should print correct values corresponding to final[i], instead of printing values like �A�. Please help.