how to find out the union of two character arrays?

Answers were Sorted based on User's Feedback



how to find out the union of two character arrays?..

Answer / prakashdasari

char *str1,*str2,*res;
while(*str1!=NULL)
str++;
while(*str2!=NULL)
{
*str1 == *str2;
str1++;
str2++;
}
*str1 = '\0';
//so total string is in one string ie str1 itself;
//now i will remove duplicates from entire string
for(i=0;str[i]!=NULL;i++)
{
for(j=0;str[j]!=NULL;j++)
{
if(str[i]==str[j])
{
flag = 1;break;
}
else flag = 0;
}
if(flag == 0)
{
*res = str[i];
res++;
}
*res = '\0';
}
now resultant string (res) is union of two character
arrays....

Is This Answer Correct ?    6 Yes 1 No

how to find out the union of two character arrays?..

Answer / om

Input: str1 (size M), str2 (size N)
Output:- Union_string str3.

1. Take a auxillary interger array of size 128 and
initialize all its's enteries to zero.(becoz All printable
chacter has value in the range [0,127]. so 128)
int Aux[128]={0}; //O(128) TIME

2. set Couter = strlen(str1) + strlen(str2) +1 ;//extra one
for '\0' character..

3. now scan the first string str1 and put 1 on the index
value equal to str1[i] in auxillary array.If there were
already 1,then Counter--;
Aux[str1[i]]=1; //O(M) TIME

4. now scan the second string str2 and put 1 on the index
value equal to str2[i] in auxillary array.Similar to step 3.
If there were already 1,then Counter--;
Aux[str2[i]=1; //O(N) TIME

5. Now alloacte a memory of size equal to "Counter" for our
resulting union string str3.
char *str3=(char*)malloc(Counter);

6. now again scan the str1 followed by str2. and if any 1 is
found in Aux[str1[i]] or Aux[str2[i]] make it 0. and put
that character to union string str3.

int k=0;
for(int i=0; str1[i]!='\0' ;i++) //O(M)
if(Aux[str1[i]] ==1)
{
str3[k++]=str1[i];
Aux[str1[i]] =0;
}

for(int i=0; str2[i]!='\0' ;i++) //0(N)
if(Aux[str2[i]] ==1)
{
str3[k++]=str2[i];
Aux[str2[i]] =0;
}
str3[k]=\0';


and return str3.


// SO TOTAL O(MAX(M,N)) TIME COMPLEXITY ALGORITHM USING
CONSTANT SPACE OF SIZE 128.

Is This Answer Correct ?    2 Yes 2 No

Post New Answer

More C Interview Questions

What is typeof in c?

0 Answers  


What is property type c?

0 Answers  


given the piece of code int a[50]; int *pa; pa=a; to access the 6th element of the array which of the following is incorrect? a.*(a+5) b.a[5] c.pa[5] d.*(*pa + 5)

6 Answers   amu, TCS,


How can I increase the allowable number of simultaneously open files?

1 Answers   CSC,


Is it acceptable to declare/define a variable in a c header?

0 Answers  






There are 21 people in a room. They have to form groups of 3 people each. How many combinations are possible? Write a C program to print the same.

1 Answers   HCL, iGate,


what does data structure mean?

8 Answers  


Write a program that an operator and two operands read from input operand operator on the implementation and results display.

0 Answers  


Meaning of () in c

1 Answers  


Dont ansi function prototypes render lint obsolete?

0 Answers  


Differentiate between full, complete & perfect binary trees.

0 Answers  


what is output? main() { #define SQR(x) x++ * ++x int i = 3; printf(" %d %d ",SQR(i),i * SQR(i)); } a)9 27 b)35 60 c)20 60 d)15 175

5 Answers   Wipro,


Categories