You are on page 1of 2

1 #include <stdio.

h>
2 #include <stdlib.h>
3
4 char* getMaximumXor(char* s, char* t)
5 {
6 unsigned int count = 0;
7 unsigned int count_1 = 0;
8 unsigned int count_0 = 0;
9 unsigned int index = 0;
10
11 char* sTemp = s;
12 char* tTemp = t;
13
14 while( *sTemp != '\0')
15 {
16 count++;
17 sTemp++;
18 }
19
20 while( *tTemp != '\0')
21 {
22 if(*tTemp == '0')
23 {
24 count_0++;
25 }
26 else
27 {
28 count_1++;
29 }
30 tTemp++;
31 }
32
33 char *ret = (char*)malloc(count);
34
35 while( (s[index] != '\0') && (t[index] != '\0') )
36 {
37 if(s[index] == '0')
38 {
39 if(count_1 > 0)
40 {
41 ret[index] = '1';
42 count_1--;
43 }
44 else
45 {
46 ret[index] = '0';
47 count_0--;
48 }
49 }
50 else if(s[index] == '1')
51 {
52 if(count_0 > 0)
53 {
54 ret[index] = '1';
55 count_0--;
56 }
57 else
58 {
59 ret[index] = '0';
60 count_1--;
61 }
62 }
63 index++;
64 }
65
66 return ret;
67 }
68
69 int main()
70 {
71 unsigned int size = 0;
72
73 printf("\nEnter size of binary string = ");
74 scanf("%d", &size);
75
76 char str1[size], str2[size];
77
78 printf("\nEnter string 1 = ");
79 scanf("%s", str1);
80 printf("\nEnter string 2 = ");
81 scanf("%s", str2);
82
83 char* ret = getMaximumXor(str1, str2);
84
85 printf("Result = %s", ret);
86
87 return 0;
88 }

You might also like