# Reverse a string while maintaining the capitalization in the same places

The goal here is to simply reverse a string, with one twist:
Keep the capitalization in the same places.

Example Input 1: Hello, Midnightas
Example Output 1: SathginDim ,olleh

Example Input 2: .Q
Exmaple Output 2: q.

Rules:

• Output to STDOUT, input from STDIN
• The winner will be picked 13th of July on GMT+3 12:00 (One week)
• The input may only consist of ASCII symbols, making it easier for programs that do not use any encoding that contains non-ASCII characters.
• Any punctuation ending up in a position where there was an upper-case letter must be ignored.
• Is that with or without the outprinting? Is that with or without the string? – user44904 Jul 6 '16 at 14:08
• "Any punctuation ending up in a position where there was an upper-case letter must be ignored.", isn't the second example inconsistent with this rule? – Stefano Sanfilippo Aug 19 '16 at 13:10
• It is consistent with the rule because punctuations do not have an uppercase variation. – user47018 Aug 19 '16 at 14:13

# Excel VBA, 256 bytes

Function z(d)
r = Len(d)
ReDim E(1 To r)
For i = 1 To r
b = Mid(d, i, 1)
If Asc(b) > 64 And Asc(b) < 91 Then E(i) = 1
Next i
i = 1
For p = r To 1 Step -1
v = Mid(d, p, 1)
If E(i) = 1 Then z = z & UCase(v) Else z = z & LCase(v)
i = i + 1
Next p
End Function


# Lua, 132 Bytes

function(s)o=""i=0 for m in s:gmatch(".")do n=s:sub(#s-i,#s-i)l=m:lower()o=o..(m==l and n:lower()or n:upper())i=i+1 end print(o)end


There might be a better way to do this in lua, but I think this works.

Try it online!

->s{i=0;s.gsub(/./){c=s[i-=1];$&=~/[A-Z]/?c.upcase: c.downcase}}  # Google Sheets, 133 Closing parens already discounted. Input: A1 • A2: =LEN(A1) • B1: =ArrayFormula(MID(A1,1+A2-SEQUENCE(A2),1)) • Reverse string • C1: =JOIN(,ArrayFormula(if(REGEXMATCH(MID(A1,SEQUENCE(A2),1),"[A-Z]"),UPPER(B:B),LOWER(B:B)))) • Compare capital positions, capitalize if originally upper, lower if not. Then join. EXACT(UPPER(...),...) is worse because we're dealing with a range here. • It's a little sluggish because we don't chop off the B:B at the blanks. # Jelly, 13 bytes ŒlṚŒuṛŒln$Tɗ¦


Try it online!

This is 100% outgolfable by someone who's actually comfortable with ¦.

# Pip, 24 bytes

{b?UCaa}MZLCRVa(_NAZ Ma)


Gets a binary array for the uppercase indices and uppercases those places in the reversed lowercase string.

Try it online!

C

#include <stdio.h>
#include <stdlib.h>
#include <string.h>

int main(int argc, char *argv[])
{
char *a,*b,*c;

a=c=strdup(argv[1]);
b=&argv[1][strlen(a)-1];
for(;*a;a++,b--){
*a=(*a>='A'&&*a<='Z')?((*b>='a'&&*b<='z')?*b-32:*b):((*b>='A'&&*b<='Z')?*b+32:*b);
}
puts(c);
free(c);
return 0;
}

• Input should be taken from stdin. – Anmol Singh Jaggi Jul 8 '16 at 4:00
• As this is code-golf please put in the number of bytes this program would cost. – user47018 Jul 13 '16 at 6:24
• I could reduce it significantly depending on the rules, but I can't find any rules. – user56095 Jul 13 '16 at 12:29