27
\$\begingroup\$

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.

Input

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.

Output

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!
Hi!tEst!

A Quick Brown Fox Jumped Over The Lazy Dog
AqUickbrOwnfoxJumpEdovertHelazYdog

testing TESTing TeStING testing testing TESTING
testingtESTIngteStInGTEstingtestingtestiNG

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

Enter        PASSWORD ---------
Enterpassword---------

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
AabbCcddEeffGghhIijjKkllMmnnOoppQqrrSsttUuvvWwxxZz

  TEST
teST
\$\endgroup\$
  • \$\begingroup\$ Sandbox \$\endgroup\$ – Stephen Jun 28 '17 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\$ – Luke Sawczak Jun 28 '17 at 21:18
  • \$\begingroup\$ @LukeSawczak thank you, my bad \$\endgroup\$ – Stephen Jun 28 '17 at 21:23
  • \$\begingroup\$ Traling spaces not allowed, I assume? \$\endgroup\$ – Luis Mendo Jun 28 '17 at 21:38
  • \$\begingroup\$ @LuisMendo your assumption is correct. This is code-golf, right? :P \$\endgroup\$ – Stephen Jun 28 '17 at 21:40

33 Answers 33

1
\$\begingroup\$

PHP, 110 bytes

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

Breakdown

$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

foreach(str_split($s)as$k=>$v)

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

echo$v<lcfirst($v)?ucfirst($q[$k]):lcfirst($q[$k])

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.

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

Charcoal, 18 bytes

⪫E⪫⪪θ ω⎇№α§θκ↥ι↧ιω

Try it online! Link is to verbose version of code. Explanation:

    θ               Input string
   ⪪                Split on spaces
  ⪫   ω             Join together
 E                  Loop over each character
          §θκ       Get character from original string
        №α          Search uppercase letters
       ⎇            If true (nonzero)
             ↥ι     Uppercase the current character
               ↧ι   Else lowercase the current character
⪫                ω  Join together
                    Implicitly print

The recent addition of StringMap shaves off a couple of bytes:

⭆⪫⪪θ ω⎇№α§θκ↥ι↧ι

is roughly equivalent to ⪫E...ω.

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

GNU sed, 67 + 1 = 68 bytes

+1 byte for -r flag.

:A
s/$/;/
:
s/([A-Z]);(.?)/;\l\1\u\2/
t
s/(\S);/;\1/
t
s/ ;//
/ /bA

Try it online!

Explanation

The algorithm is pretty simple:

  1. Insert a cursor (;) at the end of the line:

    :A
    s/$/;/
    :
    
  2. If the character before the cursor is an uppercase letter, lowercase it (\l) and uppercase the character after the cursor (\u), moving the cursor to the left, and branch back to :

    s/([A-Z]);(.?)/;\l\1\u\2/
    t
    
  3. Otherwise, if the character before the cursor is not a space, move the cursor to the left and branch back to ;:

    s/(\S);/;\1/
    t
    
  4. Otherwise, if the cursor is preceded by a space, delete both:

    s/ ;//
    
  5. Finally, if there are any spaces left, branch back to :A:

    / /bA
    
\$\endgroup\$

Your Answer

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

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