Swap capitalization of two strings
CJam, 25 bytes
{z{_el_eu&\__:^32&f^?}%z}
This is an anonymous function that pops an array of strings from the stack and leaves one in return.
In supported browsers, you can verify all test cases at once in the CJam interpreter.
Test cases
Code
qN/2/ e# Read input and split into arrays of two strings.
{z{_el_eu&\__:^32&f^?}%z}
% e# Map the block over all string arrays.
:+N* e# Separate the strings by linefeeds.
Input
ABCDEfghijKlMnOpqrstuvwxyz
aaaaaaaaaaaaaaaa----------
PRogrammiNG puzZLes & CODe golf
SdlkhkfaladlKsdlalksdg7ldklDgsl
AAAbbb111
Cc2Dd3Ee4
Output
abcdefghijklmnopqrstuvwxyz
AAAAAaaaaaAaAaAa----------
Programming Puzzles & Code Golf
SDlkhkfalADlksdLAlksdg7LDkldgsl
AaABbb111
CC2dd3Ee4
How it works
z e# Zip to transform the array of strings into an array
e# of character pairs.
{ }% e# For each character pair:
_el e# Push a copy and convert to lowercase.
_eu e# Push a copy and convert to uppercase.
& e# Intersect. The result will be an empty string if
e# and only if both characters are letters.
\ e# Swap the character pair on top of the stack.
__ e# Push two copies.
:^ e# XOR both characters.
32& e# Logical AND with 32. This pushes 32 for letters of
e# different cases and 0 for letters of the same case.
f^ e# XOR each character with the result.
? e# Select the original copy for non-empty intersection
e# and the modified one otherwise.
z e# Zip to turn the characters pairs back into two strings.
C, 126 bytes
This is my first attempt at a code golf, ever. Let me know if I did anything wrong.
I'm using bitwise operations to perform the switching
Golfed:
main(u,a,s,t)char**a,*s,*t;{for(s=a[1],t=a[2];*t;s++,t++)isalpha(*s)*isalpha(*t)?u=(*t^*s)&32,*t^=u,*s^=u:0;*s=10;puts(a[1]);}
Ungolfed:
main(u,a,s,t) char**a,*s,*t; { // K&R style arguments
for(s=a[1],t=a[2];*t;s++,t++) // initialize loop.
isalpha(*s) * isalpha(*t) ? // ensure both characters are letters (if)
u = (*t^*s) & 0x20, // check if characters have swapped case
*t^=u, // if so, xor the bit which represents case
*s^=u // for both characters in the string.
:0; // end ternary statement (endif)
*s=10; // replace null terminator in first string
puts(a[1]); // with newline. This allows both output to
} // be printed out all at once
edit: replaced && with *
Pyth, 19 18 bytes
LCmrW&@dG@drG1d2Cb
This defines a function y that accepts and return a list of strings.
Verify all test cases at once in the Pyth Compiler/Executor.
Thanks to @Jakube for golfing off 1 byte.
How it works
" (implicit) Initialize G to 'abcdefghijklmnopqrstuvwxyz'.
L " Define y(b):
Cb " Zip to turn the two strings into an array of char pairs.
m " Map (implicit variable d):
@dG " Intersect d with G.
@drG1 " Intersect d with G.upper().
W& " If both are non-empty:
r d2 " Apply swapcase() to d.
C " Zip to turn the character pairs back into two strings.