XOR Strings 2

  • + 1 comment

    This function performs a bitwise XOR operation on two input strings s and t.

    The XOR operation compares corresponding characters of the two strings:

    - If the characters are the same, it appends '0' to the result string.

    - If the characters are different, it appends '1' to the result string.

    def strings_xor(s, t): res = "" # Initialize an empty result string

    # Loop through each character index in the strings (assumes both strings have the same length)
    for i in range(len(s)):
        if s[i] == t[i]:  
            res += '0'  # If characters at index `i` are the same, append '0'
        else:
            res += '1'  # If characters at index `i` are different, append '1'
    
    return res  # Return the final XOR result string
    

    Input: Two binary strings from the user

    s = input() # First binary string t = input() # Second binary string

    Output: The XOR of the two input strings

    print(strings_xor(s, t))