27
\$\begingroup\$

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.
\$\endgroup\$
  • \$\begingroup\$ Is that with or without the outprinting? Is that with or without the string? \$\endgroup\$ – user44904 Jul 6 '16 at 14:08
  • \$\begingroup\$ "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? \$\endgroup\$ – Stefano Sanfilippo Aug 19 '16 at 13:10
  • \$\begingroup\$ It is consistent with the rule because punctuations do not have an uppercase variation. \$\endgroup\$ – user47018 Aug 19 '16 at 14:13

37 Answers 37

1
2
0
\$\begingroup\$

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
\$\endgroup\$
0
\$\begingroup\$

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.

\$\endgroup\$
0
\$\begingroup\$

Ruby, 64 bytes

Try it online!

->s{i=0;s.gsub(/./){c=s[i-=1];$&=~/[A-Z]/?c.upcase: c.downcase}}
\$\endgroup\$
0
\$\begingroup\$

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.
\$\endgroup\$
0
\$\begingroup\$

Jelly, 13 bytes

ŒlṚŒuṛŒln$Tɗ¦

Try it online!

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

\$\endgroup\$
0
\$\begingroup\$

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!

\$\endgroup\$
-1
\$\begingroup\$

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;
}
\$\endgroup\$
  • \$\begingroup\$ Input should be taken from stdin. \$\endgroup\$ – Anmol Singh Jaggi Jul 8 '16 at 4:00
  • \$\begingroup\$ As this is code-golf please put in the number of bytes this program would cost. \$\endgroup\$ – user47018 Jul 13 '16 at 6:24
  • \$\begingroup\$ I could reduce it significantly depending on the rules, but I can't find any rules. \$\endgroup\$ – user56095 Jul 13 '16 at 12:29
1
2

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy