Skip to main content
We’ve updated our Terms of Service. A new AI Addendum clarifies how Stack Overflow utilizes AI interactions.
Code Golf

You are not logged in. Your edit will be placed in a queue until it is peer reviewed.

We welcome edits that make the post easier to understand and more valuable for readers. Because community members review edits, please try to make the post substantially better than how you found it, for example, by fixing grammar or adding additional resources and hyperlinks.

Required fields*

Required fields*

XOR two strings

Given two strings as input, return the result of XORing the code-points of one string against the code points of the other.

For each character in the first input string, take the code-point (e.g. for A, this is 65) and XOR the value against the corresponding index in the second string and output the character at the code-point of the result. If one string is longer than the other, you must return the portion of the string beyond the length of the shorter, as-is. (Alternatively, you may pad the shorter string with NUL bytes, which is equivalent.)

See the following JavaScript code for an example:

const xorStrings = (a, b) => {
 let s = '';
 // use the longer of the two words to calculate the length of the result
 for (let i = 0; i < Math.max(a.length, b.length); i++) {
 // append the result of the char from the code-point that results from
 // XORing the char codes (or 0 if one string is too short)
 s += String.fromCharCode(
 (a.charCodeAt(i) || 0) ^ (b.charCodeAt(i) || 0)
 );
 }
 return s;
};

Try it online!

Test cases

Input Output
['Hello,', 'World!'] '\x1f\x0a\x1e\x00\x0b\x0d'
['Hello', 'wORLD'] '?*> +'
['abcde', '01234'] 'QSQWQ'
['lowercase', "9?' "] 'UPPERCASE'
['test', ''] 'test'
['12345', '98765'] '\x08\x0a\x04\x02\x00' _not_ 111092
['test', 'test'] '\x00\x00\x00\x00'
['123', 'ABCDE'] 'pppDE'
['01', 'qsCDE'] 'ABCDE'
['`c345', 'QQ'] '12345'

Rules

  • The two input strings will only ever be code-points 0-255.
  • This is so the shortest solution, in each language, wins.

Answer*

Draft saved
Draft discarded
Cancel
8
  • \$\begingroup\$ You're relying on the first string storing as much memory as the second string in case the second string is longer than the first. That's not really playing fair. Certainly not how strings work in C. \$\endgroup\$ Commented Jul 20, 2020 at 1:15
  • 1
    \$\begingroup\$ You're also relying on memory past the end of the shorter string to be initialised to zero. \$\endgroup\$ Commented Jul 20, 2020 at 1:23
  • \$\begingroup\$ I agree it isn't the fairest, but given how strings are represented in C there isn't a much cleaner way to write this as a function. Your solution doesn't provide any means by which to access the new string, other than trapping the output stream. The only truly portable way would be to take 6 parameters: a, len(a), b, len(b), buffer, len(buffer). But defensive coding has never really been the goal of these submissions ;) \$\endgroup\$ Commented Jul 20, 2020 at 20:27
  • \$\begingroup\$ There are many ways to make your code cleaner. Not reading past the end of strings for starters. The point is that work is being done outside of your function to make it work. To change mine to return a string is trivial and certainly doesn't involve passing string lengths. Simply malloc a string buffer based on the maximum strlens of the inputs and return that pointer after creating the result there. \$\endgroup\$ Commented Jul 20, 2020 at 21:38
  • \$\begingroup\$ same can be done in mine. it also still requires work outside of the function in terms of free. A solution would be something like this: Try it online! \$\endgroup\$ Commented Jul 20, 2020 at 22:32

AltStyle によって変換されたページ (->オリジナル) /