[// C++ Implementation to find the
02
// XOR of the two Binary Strings
03
04
#include<bits/stdc++.h>
05
usingnamespace std;
06
07
// Function to find the
08
// XOR of the two Binary Strings
09
string xoring(string a, string b, int n){
10
string ans = "";
11
12
// Loop to iterate over the
13
// Binary Strings
14
for (int i = 0; i < n; i++)
15
{
16
// If the Character matches
17
if (a[i] == b[i])
18
ans += "0";
19
else
20
ans += "1";
21
}
22
return ans;
23
}
24
25
// Driver Code
26
int main()
27
{
28
string a = "1010";
29
string b = "1101";
30
int n = a.length();
31
string c = xoring(a, b, n);
32
cout << c << endl;
33
} ]
#include<stdio.h>
02
03
string xoring(string a, string b, int n)
04
05
string ans = "";
06
07
for (int i = 0; i < n; i++)
08
{
09
10
if (a[i] == b[i])
11
ans += "0";
12
else
13
ans += "1";
14
}
15
return ans;
16
17
18
// Driver Code
19
int main()
20
{
21
string a = "1010";
22
string b = "1101";
23
int n = a.length();
24
string c = xoring(a, b, n);
25
printf("c");
26
}
but getting errors :
main.c:3:1: error: unknown type name ‘string’
string xoring(string a, string b, int n)
main.c:21:5: error: unknown type name ‘string’
string a[] = "1010";
main.c:23:13: warning: initialization makes integer from pointer without a cast [-Wint-conversion]
main.c:24:5: error: unknown type name ‘string’
string c = xoring(a, b, n);
this begs a rewrite.
converting code ends up making a huge mess. Just do it from scratch and it will be more natural, and work better. Only convert code if there is no other alternative.
C has only primitive type (builtin type in compiler) , no user-defined type at all such as string, vector, array etc, all of which actually bulk of classes packed in a library
so you must use pointers to char type processes and manipulations, to accomplish it to achieve the same result
it has user defined types: struct and typedef are both available. Function pointes allow you to make 'methods' for structs, as well, in a crude fashion. Not needed here, as the c char array tools are sufficient.