XOR de dos strings binarias

Dadas dos strings binarias A y B de igual longitud, la tarea es imprimir una string que sea el XOR de las strings binarias A y B.
Ejemplos: 
 

Entrada: A = “0001”, B = “0010” 
Salida: 0011
Entrada: A = “1010”, B = “0101” 
Salida: 1111 
 

Enfoque: la idea es iterar sobre la string carácter por carácter y, si el carácter no coincide, agregar «1» como carácter en la string de respuesta; de lo contrario, agregar «0» a la string de respuesta para generar la string XOR.
 

A continuación se muestra la implementación del enfoque anterior:
 

C++

// C++ Implementation to find the
// XOR of the two Binary Strings
#include<bits/stdc++.h>
using namespace std;
 
// Function to find the
// XOR of the two Binary Strings
string xoring(string a, string b, int n){
string ans = "";
     
    // Loop to iterate over the
    // Binary Strings
    for (int i = 0; i < n; i++)
    {
        // If the Character matches
        if (a[i] == b[i])
            ans += "0";
        else
            ans += "1";
    }
    return ans;
}
 
// Driver Code
int main()
{
    string a = "1010";
    string b = "1101";
    int n = a.length();
    string c = xoring(a, b, n);
    cout << c << endl;
}
 
// This code is contributed by Surendra_Gangwar

Java

// Java Implementation to find the
// XOR of the two Binary Strings
import java.io.*;
 
class GFG {
    // Function to find the
    // XOR of the two Binary Strings
    static String  xoring(String a, String b, int n){
    String ans = "";
         
        // Loop to iterate over the
        // Binary Strings
        for (int i = 0; i < n; i++)
        {
            // If the Character matches
            if (a.charAt(i) == b.charAt(i))
                ans += "0";
            else
                ans += "1";
        }
        return ans;
    }
     
    // Driver Code
    public static void main (String[] args)
    {
        String a = "1010";
        String b = "1101";
        int n = a.length();
        String c = xoring(a, b, n);
        System.out.println(c);
    }
}
 
// This code is contributed by shubhamsingh10

Python3

# Python Implementation to find the
# XOR of the two Binary Strings
 
# Function to find the
# XOR of the two Binary Strings
def xor(a, b, n):
    ans = ""
     
    # Loop to iterate over the
    # Binary Strings
    for i in range(n):
         
        # If the Character matches
        if (a[i] == b[i]):
            ans += "0"
        else:
            ans += "1"
    return ans
 
# Driver Code
if __name__ == "__main__":
    a = "1010"
    b = "1101"
    n = len(a)
    c = xor(a, b, n)
    print(c)

C#

// C# Implementation to find the
// XOR of the two Binary Strings
using System;
 
class GFG{
    // Function to find the
    // XOR of the two Binary Strings
    static string xoring(string a, string b, int n){
    string ans = "";
         
        // Loop to iterate over the
        // Binary Strings
        for (int i = 0; i < n; i++)
        {
            // If the Character matches
            if (a[i] == b[i])
                ans += "0";
            else
                ans += "1";
        }
        return ans;
    }
     
    // Driver Code
    static public void Main ()
    {
        string a = "1010";
        string b = "1101";
        int n = a.Length;
        string c = xoring(a, b, n);
        Console.WriteLine(c);
    }
}
 
// This code is contributed by shubhamsingh10

Javascript

<script>
 
// Javascript Implementation to find the
// XOR of the two Binary Strings
 
// Function to find the
// XOR of the two Binary Strings
function xoring(a, b, n){
let ans = "";
     
    // Loop to iterate over the
    // Binary Strings
    for (let i = 0; i < n; i++)
    {
        // If the Character matches
        if (a[i] == b[i])
            ans += "0";
        else
            ans += "1";
    }
    return ans;
}
 
// Driver Code
    let a = "1010";
    let b = "1101";
    let n = a.length;
    let c = xoring(a, b, n);
    document.write(c);
 
</script>

Producción:

0111

Publicación traducida automáticamente

Artículo escrito por AmiMunshi y traducido por Barcelona Geeks. The original can be accessed here. Licence: CCBY-SA

Deja una respuesta

Tu dirección de correo electrónico no será publicada. Los campos obligatorios están marcados con *