Your input will be an English sentence, phrase, or word. It will only contain a-zA-Z' -,.!?. Your task is to take the input, remove spaces, and then redistribute capitalization such that letters at indexes that were capitalized before (and only letters at indexes that were capitalized before) are capitalized.

For example, if the input is A Quick Brown Fox Jumped Over The Lazy Dog, the (0-based) indexes of the capital letters are 0, 2, 8, 14, 18, 25, 30, 34, 39. Next, remove spaces from the input: AQuickBrownFoxJumpedOverTheLazyDog. Next, lowercase all letters, but uppercase those at 0, 2, 8, 14, 18, 25, 30, 34, 39: AqUickbrOwnfoxJumpEdovertHelazYdog, which is your output.


Your input will be an English sentence, phrase, or word. It can only contain lowercase letters, uppercase letters, hyphens, apostrophes, commas, periods, question marks, exclamation marks, and spaces.


The input with spaces removed, lowercase-d, with letters at the index of capital letters in the input uppercase-d.

NOTE: Your program cannot crash (error such execution terminates) with an IndexOutOfRange or similar error.

Test Cases

Hi! Test!

A Quick Brown Fox Jumped Over The Lazy Dog

testing TESTing TeStING testing testing TESTING

TESTING... ... ... success! EUREKA???!!! maybe, don't, NOOOOO

Enter        PASSWORD ---------

A a B b C c D d E e F f G g H h I i J j K k L l M m N n O o P p Q q R r S s T t U u V v W w X x Z z

  • \$\begingroup\$ Sandbox \$\endgroup\$
    – Stephen
    Jun 28, 2017 at 21:06
  • \$\begingroup\$ 'For example, if the input is "A Quick Brown Fox Jumped Over The Lazy Dog", the (0-based) indexes of the capital letters are 0, 2, 8, 14, 18, 23, 27, 32' They are 0, 2, 8, 14, 18, 25, 30, 34, 39 \$\endgroup\$ Jun 28, 2017 at 21:18
  • \$\begingroup\$ @LukeSawczak thank you, my bad \$\endgroup\$
    – Stephen
    Jun 28, 2017 at 21:23
  • \$\begingroup\$ Traling spaces not allowed, I assume? \$\endgroup\$
    – Luis Mendo
    Jun 28, 2017 at 21:38
  • \$\begingroup\$ @LuisMendo your assumption is correct. This is code-golf, right? :P \$\endgroup\$
    – Stephen
    Jun 28, 2017 at 21:40

38 Answers 38


Python 3, 125, 124 bytes

lambda s:''.join(c.upper()if i in(s.find(q)for q in s if q.isupper())else c for i,c in enumerate(s.replace(' ','').lower()))

Try it online!


Python 2, 106 105 bytes

print''.join(map(lambda(c,u):[c.lower,c.upper][u](),zip(s.replace(' ',''),map(str.isupper,s))))

Edit: save one byte via print ''.join => print''.join.

Lambda form, 99 bytes

lambda s:''.join(map(lambda(c,u):[c.lower,c.upper][u](),zip(s.replace(' ',''),map(str.isupper,s))))

SCALA, 128 chars, 128 bytes

var l=s.toLowerCase().filter(x=>x!=32)
for(i<-0 to l.size-1){if(s(i).isUpper)l=l.substring(0,i)+l(i).toUpper+l.substring(i+1)}

Thanks for this challenge. Try it online!


q/kdb+, 49 bytes


{@[a;(&)#:[a:lower x except" "]#x in .Q.A;upper]}


q){@[a;(&)#:[a:lower x except" "]#x in .Q.A;upper]}"Hi! Test!"

q){@[a;(&)#:[a:lower x except" "]#x in .Q.A;upper]}"A Quick Brown Fox Jumped Over The Lazy Dog"

q){@[a;(&)#:[a:lower x except" "]#x in .Q.A;upper]}"testing TESTing TeStING testing testing TESTING"

q){@[a;(&)#:[a:lower x except" "]#x in .Q.A;upper]}"TESTING... ... ... success! EUREKA???!!! maybe, don't, NOOOOO"

q){@[a;(&)#:[a:lower x except" "]#x in .Q.A;upper]}"Enter        PASSWORD ---------"

q){@[a;(&)(#:[a:lower x except" "]#x)in .Q.A;upper]}"A a B b C c D d E e F f G g H h I i J j K k L l M m N n O o P p Q q R r S s T t U u V v W w X x Z z"

q){@[a;(&)#:[a:lower x except" "]#x in .Q.A;upper]}"  TEST"


Find indices where input is uppercase and then apply function upper to those indices on a lowercase, space-removed version of the input string. Note that we cannot apply the function beyond the length of the string, so use take (#) to truncate the input string to length of the lowercase, space-removed version.

{@[a;where count[a:lower x except " "]#x in .Q.A;upper]} / ungolfed
{                                                      } / lambda function
 @[ ;                                           ;     ]  / apply FUNC to VAR at INDICES: @[VAR;INDICES;FUNC]
                                                 upper   / uppercase, upper["abc"] -> "ABC"
                                       x in .Q.A         / boolean list where input is in uppercase alphabet ABC..XYZ
                                      #                  / take this many elements from list on the right (ie truncate)
           count[                    ]                   / returns length of the stuff inside the brackets, count["ABC"] -> 3                                        
                         x except " "                    / remove " " from string
                   lower                                 / lowercase, lower["ABC"] -> "abc"
                 a:                                      / save in variable a
     where                                               / returns indices where true where[101b] -> 0 2
   a                                                     / our lowercased, space-stripped input


After reading the answers, thought I'd try a solution where I iterate over the input, so far I've only managed a 53 byte solution:

{a{$[y in .Q.A;upper x;x]}'#:[a:lower x except" "]#x}

Swift 3.0, 199 bytes

var s="AS Ff",i=[String](),p=[Int](),j=0;for c in s.characters{if c>="A"&&c<="Z"{p.append(j)};if c != " "{i.append(String(c).lowercased())};j=j+1};for c in p{i[c]=i[c].uppercased()};print(i.joined())

Try it Online!


PHP, 110 bytes

$q=strtr($s=$argn,[' '=>'']);foreach(str_split($s)as$k=>$v)echo$v<lcfirst($v)?ucfirst($q[$k]):lcfirst($q[$k]);


$q=strtr($s=$argn,[' '=>''])

We take our input ($argn) and store it in a smaller variable because we need it later, before stripping out all the spaces with strtr


We then take our original string, split it into an array of characters, and loop through each of them.


We check to see if the current letter is uppercase (A is less than a probably because ASCII), and echo out the corresponding letter from the space-stripped string with the appropriate case.


Jelly, 11 bytes


Try it online!

Just a restructuring of Dennis' original solution to avoid repeating Œl.

Œl             Lowercase the input.
  ð            Make that the left argument to the following dyadic chain.
   Ḳ           Split on spaces,
    F          flatten out the splits,
     Œu        then uppercase that
       T}¦     at the indices where
Œlð       n    the lowercase doesn't equal the original.

Vyxal, 4 bytes


Try it Online!

Helps to have a "with capitalisation of" built-in


ȧ     # ‎⁡Remove whitespace from the input
 ⇩    # ‎⁢and lowercase it
  $•  # ‎⁣Transfer the capitalisation of the original input to the stripped input.
# ‎⁤The reason for `•` doing this is because on lists, `•` is mould. Therefore, the idea was that it "moulds" one string to the capitalization of the other

Created with the help of Luminespire.


Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.