[Question]
  [
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
```
  
[Answer]
# [C (gcc)](https://gcc.gnu.org/), ~~82~~ ~~79~~ ~~74~~ ~~72~~ ~~69~~ ~~67~~ 66 bytes
```
f(c){for(char*s=c,*p=c;c=*s++;c&&putchar(c^(*p++|~c/2)&32))c&=95;}
```
[Try it online!](https://tio.run/##bZBNT8JgEITP8iuecqgtBUwwHkxDCEgVAUFoEePBpLwUqEhb@wGC4l/HYiDxwCSb3cxkZjcrClMhdruJItSviR8qYmaHuags8rmgLHRRzkWapgtZDpJ4LyniVckFmvb9Iy5KqnxZUlUhl6@v9O1uYbueoma@MmcTJdtwJSwniqWsqmfOUnOkZP/GVKvSS1wxpxb6K49b/5NmsgicMd2lE2LNHNr2Zk3dn54wx2mo602xDNP6644Z33fuOPL/9ZQ/kXBQisUix4oSIZwokjAGfaNVrVQqkiSxsNcjJ8/Y987jPJ3uHifyDC9Ozz7gsWqaw26/TuGIkw@wqTHiBkGdMQYOt0y4Y0qDGfe4NHmjxZw27zywoINHF59HAnp80CfEJMIiZkDCE0uGrHjmkxc2@1Xb3S8 "C (gcc) – Try It Online")
[Answer]
# [Jelly](https://github.com/DennisMitchell/jelly), ~~14~~ 13 bytes
```
nŒlTɓḲFŒlŒuṛ¦
```
[Try it online!](https://tio.run/##y0rNyan8/z/v6KSckJOTH@7Y5AZkHZ1U@nDn7EPL/v//76gQWJqZnK3gVJRfnqfgll@h4FWaW5CaouBfllqkEJKRquCTWFWp4JKfDgA "Jelly – Try It Online")
### How it works
```
nŒlTɓḲFŒlŒuṛ¦  Main link. Argument: s (string)
 Œl            Convert s to lowercase.
n              Perform character-wise "not equal" comparison.
   T           Get the indices of all truthy elements, i.e., the indices of all
               uppercase letters in s. Let's call the resulting array J.
    ɓ          Begin a dyadic chain with left argument s and right argument J.
     ḲF        Split s at spaces and flatten, removing the spaces.
       Œl      Convert s to lowercase.
            ¦  Sparse application:
         Œu        Convert s to uppercase.
           ṛ       Take the resulting items of the uppercased string at all indices
                   in J, the items of the lowercased string at all others.
```
[Answer]
# [Python 2](https://docs.python.org/2/), 114 bytes
```
x=input()
X=x.replace(' ','')
print''.join([X[i].upper()if x[i].isupper()else X[i].lower()for i in range(len(X))])
```
[Try it online!](https://tio.run/##LcqxCsIwEIDhvU9xdEkOJIN7JwdfIVA6FL3oSbg70hTj00cqjv/Hb5/6VDn33iYW26vHIU4tFLK83sg7cCfncLDCUp0LL2Xxc5x5CbsZFY@coB3J2x8obwS/I@v7gKQFGFigrPIgn0l8RFyw9/Gid4Kr5jR@AQ "Python 2 – Try It Online")
# Equivalently:
# [Python 2](https://docs.python.org/2/), 114 bytes
```
lambda x:''.join([[str.lower,str.upper][x[i].isupper()](x.replace(' ','')[i])for i in range(len(x)-x.count(' '))])
```
[Try it online!](https://tio.run/##HYoxDsIwDEWvks22VDIwIjExcIiQIbQOBIU4clsRTh9atvfe//W7PKUcezzfeg7v@xRMOwHYl6SCzs2L2iwf1mGntVZW75pL3qb5b0gem1WuOYyMYGAAoG2nKGqSScVoKA/GzAUbHZodZS3LfiTy1KumzSLCRSY2V8lx6/0H "Python 2 – Try It Online")
[Answer]
# [Python 3](https://docs.python.org/3/), ~~78~~ ~~75~~ 72 bytes
```
s=input()
for c in s:s=s[c>' '!=print(end=(c+c).title()[s<'@'or'['<s]):]
```
*Thanks to @xnor for golfing off 6 bytes!*
[Try it online!](https://tio.run/##TZBLT8JAFIX3/RVfV9MqsjFxQaiIUt6C0ipG4gLLABWY1s4UwT@P9UHil9yc5NxHTm66N8tEnR/kTkaOEOKgvViluXFca55kRMQKXdGenkSXAmF7aRYr40g185zoNHLLJjZr6bgTXRVXIsnERFT1i1t5OfwOusVNTrhwD@3YJpTa2Fad@zyOVlxnyYeimezo5ptUzhhuZUa4lPSnn3saycIyxUKsFoR@EP6oDExn0OLo/@8XvvWn5XKZY@k8iqTWNv7DyO/Va7WabdtspvtXWWKWKGFKDIbfWL4yRYA/7upBMB6OGpwdKYJPueaVm@IvDWb4SJrMabGgzZIOMV3e6LGiz5pbNgxQDEm4I@Wed0ZkBGhCDA/kPLJlzAdP7Hjm8ws "Python 3 – Try It Online")
[Answer]
# [05AB1E](https://github.com/Adriandmen/05AB1E), ~~15~~ 14 bytes
-1 byte thanks to [Emigna](https://codegolf.stackexchange.com/users/47066/emigna)
```
ðKuvy¹Nè.lil}?
```
[Try it online!](https://tio.run/##BcFJcgEBAADAr/QLFAdnZYtdRATlooxdhtiDKv9xdHXlYaM7nhwEiXEUve6V8Hh@PuqvWyzszw/XVBSlDWQEsoZyRvLGPkwUTBXNlMyVLVT8qgrVLNWtfPrTsPZlo2nr207L3o@DtqOOf10nPZc3 "05AB1E – Try It Online")
```
ðK             # Remove spaces
  u            # Convert to uppercase
   vy          # For each character...
     ¹Nè       #   Get the character at the same index from the original input
        .lil}  #   If it was a lowercase letter change this one to lowercase
             ? # Print without a newline
```
[Answer]
## [Haskell](https://www.haskell.org/), ~~98~~ ~~95~~ ~~89~~ ~~88~~ 81 bytes
*Thanks to @name, @nimi, @Zgarb, and @Laikoni for helping shave off 14 bytes total*
```
import Data.Char
\s->zipWith(\p->last$toLower:[toUpper|isUpper p])s$filter(>' ')s
```
Ungolfed:
```
import Data.Char
\sentence -> zipWith (\oldChar newChar ->
                        if isUpper oldChar
                        then toUpper newChar
                        else toLower newChar)
                     sentence
                     (filter (/= ' ') sentence)
```
[Answer]
# Python 2, 100 bytes
```
s=input()
print"".join([c.lower(),c.upper()][s[i].isupper()]for i,c in enumerate(s.replace(" ","")))
```
[Answer]
# [V](https://github.com/DJMcMayhem/V), 24 bytes
```
ÄVuÓó
ejlDò/¥2lõ
vuk~òGd
```
[Try it online!](https://tio.run/##BcFJEsFAGIDRfU7x3UCVGyDEPASh7JAYkjYE3UlZWLmBM7hAlAv4D9beM9bKM9Dyko8TxcqVovR7l5V8HaOThxReaG2FJVVW1FjjElInosEGjy1NdrTY0yamQ0IXRY8DfY4MODHkzIgUnwtjrky4MUUTYJiRMSdnwf0P "V – Try It Online")
These kind of challenges are exactly what V was made for. :)
Explanation:
```
Ä           " Duplicate this line
 Vu         " Convert it to lowercase
   Óó       " Remove all spaces
e           " Move to the end of this line
 j          " Move down a line (to the original)
  l         " Move one char to the right
   D        " And delete the end of this line
    ò       " Recursively:
     /      "   Search for:
         õ  "     An uppercase character
      ¥2l   "     On line 2
            "     (This will break the loop when there are no uppercase characters left)
vu          "   Convert it to lowercase
  k         "   Move up a line
   ~        "   Convert this to uppercase also
    ò       " Endwhile
     G      " Move to the last line
      d     " And delete it
```
[Answer]
# JavaScript (ES6), ~~94~~ ~~91~~ ~~85~~ ~~84~~ 81 bytes
```
s=>s.replace(r=/./g,c=>1/c?"":c[`to${r<s[x]&s[x++]<{}?"Upp":"Low"}erCase`](),x=0)
```
* 6 bytes saved with assistance from ETHproductions & Arnauld.
* 3 bytes saved thanks to l4m2
[Try it online!](https://tio.run/##XY9rT8JAEEW/@ytOG6MQsOg3YywNSn0iKC1iJCTWZcGqtHV3eajxt2M1YsSbzExyJnNn5jGaRlqoODNb093F0F1ot6odJbPnSMiCcitOZVQWbnWnIjzb3hO9O5Ouv6t93Zv3N/JUKvX33z88u5Nl9p7dSGf2h1SHkZZ3/UKxPHe3i4tMxYkpDAv2SWwRSm0su1hc@6U1riaxeOJApbOEo3TO2WScyQGtqVSED5JG9PZKPR2tjJncKE5GhH4QflcZmNPmMUv@t5/zldkf5jgOy9ATIaTWFn6n7Z/XPM@zLItx9HovywzSZNOUaba@tOLkJyY/8keXtSDottp1tpb692jEAfccIqgzwEdyxJBjRpzwwCkxZzxyzhMNnrlgTJOEFimXZFzxQhtFgCbE0GHCNVO6zLhhzi1vK/v4/j1Hi08)
[Answer]
# [Alice](https://github.com/m-ender/alice), 32 bytes
```
/..- ~l+u~mSloy
\ia''-y.'Qa.+a@/
```
[Try it online!](https://tio.run/##S8zJTE79/19fT09XoS5Hu7QuNzgnv5IrJjNRXV23Uk89MFFPO9FB////ktTiksy8dIUQ1@AQMJ0aXOLp564AE0eWB4oDAA "Alice – Try It Online")
## Explanation
This is a standard template for programs that work entirely in ordinal mode. Unwrapped, the program is as follows:
```
i.' -l.uQm.lay.a-'~y+'~aS+o@
i       take input as string
.       duplicate
' -     remove spaces from copy
l.u     create all-lowercase and all-uppercase versions
Q       reverse stack, so original string is on top
m       truncate original string to length of spaces-removed string
.lay    convert everything except uppercase characters to \n
.a-'~y  convert everything except \n (i.e., convert uppercase characters) to ~
+       superimpose with lowercase string
        \n becomes the corresponding lowercase character, and ~ remains as is
'~aS    convert ~ to \n
+       superimpose with uppercase string
        lowercase in existing string stays as is because it has a higher code point
        \n becomes corresponding uppercase character
o       output
@       terminate
```
[Answer]
# [Retina](https://github.com/m-ender/retina), ~~77~~ 71 bytes
```
.+
$&¶$&
T`L `l_`.+$
+`((.)*)[A-Z].*(¶(?<-2>.)*)
$1$3 
.+¶
T`l `L_` .?
```
[Try it online!](https://tio.run/##TY7ZTsJQFEXf71esJhULhZuor8YGpUwiU@sQjaEMV6hAq7TI8GF8AD9Wi4HElZzsZJ3sZC9U7Af95MyoeIk0hZ7Z7/SMcL0G3qznSVMXpmcYMpvLvhULr@8yZ@x3hnVduLw5OKFf6FcIae53Ii3N8Bo9D2klSdXXcFUUa6JIZ@kPp9wuwlVAOVxTX86/1IjWj1rgThSN/nZDKRyLOC34wRjXdty/VE5ca1Y4@f//1ItjSik5XbQcDlUUadiPXfu@aFmWpmnM@5uByjMKg/M4T7N1QNhBnA440i46znOrW6JwIh3e55YBdwwpMcJGUeaDCmOqTKjhU@eTe6Y0mPHAnCYBLULafNHhmy4LHCJcYh5Z8sQPz6x4Yc0r218 "Retina – Try It Online") Link includes test suite. Explanation: The first stage duplicates the line while the second stage lowercases the duplicate and deletes its spaces. The third stage then loops through each uppercase letter from right to left and attempts to place a space before the corresponding character on the second line. The first line is deleted and the spaces are used to uppercase the relevant characters of the result. Edit: Saved 6 bytes thanks to @Kobi.
[Answer]
# Java 8, ~~184~~ ~~177~~ 161 bytes
```
s->{String r="";for(int i=0,j=i,t,u;i<s.length;){t=s[i++];if(t>32){u=s[j++];r+=(char)(t<65|t>90&t<97|t>122?t:u>64&u<91?t&~32:u>96&u<123|u<33?t|32:t);}}return r;}
```
Can definitely be golfed some more..  
- 16 bytes thanks to *@OlivierGrégoire* by taking the input as `char[]` instead of `String`.
**Explanation:**
[Try it here.](https://tio.run/##lVJNb9pAEL3nVzxzoLYAK0BLRY2xSEK@AwkmTVXEwZgFlg@b7s6SEHD/Ol0ISD20B0Y72p03OzNvRjMOFkEunrNo3J9swmkgJR4CHq1OAB4RE4MgZGhsTcAnwaMhQjMcBaLTlZaj4USrPpIC4iEaiOBiI3PV1f63cFMpZxALU6cDd0@zY5dnKascXpH2lEVDGjnWilzZ4ZlM1@EDk6rFgrVSGhlvEZFxdwUtkyqlL2uqlk/TVCl/1a98oeDRN1UtfU6rSjnvUfp3saDtcknb@UJxrSrFokdrDZLlJIlgpEQE4SQb54P2XPWmmvae/SLmfcx0@@YH@U4XgbXvfSmJzexYkT3XLppGZmSHZuqaG2gzSUbKpvhc06wJESxNy9oN5/9xNTwpHk5wJuLXCJfxG27VbM76aC6YQHvEcB@8L3ERD49MTJrMdu7tut/e3cynm8YVDvjffo0fmX0fZds2DipVGDIpDdSfW/W7mud5hmHoKS57LIt@HH2iLBrNrRxZq77dP@zlseb7L83WBXIHOXrgAc7QwzlCXKCPOhguMcAVhrjGCDfguMUYd5jgHlM8YLbb5iZiPGKOJ/xCCwI@JNogPEPhOxZ4wSt@4A0/8f5PRslJsvkD)
```
s->{                           // Method with char-array parameter and String return-type
  String r="";                 //  Result-String
  for(int i=0,j=i,t,u;         //  Some temp integers and indices
      i<s.length;){            //  Loop over the String
    t=s[i++];                  //   Take the next character and save it in `t` (as integer)
                               //   and raise index `i` by 1
    if(t>32){                  //   If `t` is not a space:
     u=s[j++];                 //   Take `u` and raise index `j` by 1
     r+=                       //   Append the result-String with:
      (char)                   //    Integer to char conversion of:
       (t<65|t>90&t<97|t>122?  //     If `t` is not a letter:
        t                      //      Simply use `t` as is
       :u>64&u<91?             //     Else if `u` is uppercase:
        t&~32                  //      Take `t` as uppercase
       :u>96&u<123|u<33?       //     Else if `u` is lowercase or a space:
        t|32                   //      Take `t` as lowercase
       :                       //     Else:
        t);                    //      Take `t` as is
    }
  }                            //  End of loop
  return r;                    //  Return result-String
}                              // End of method
```
[Answer]
# Perl, ~~95~~ 94 + 1 = 95 bytes
+1 byte penalty for -n
Save one byte by replace from `s/\s//g` to `s/ //g`
```
$s=$_;s/ //g;$_=lc($_);while(/(.)/gs){$p=$&;$p=uc($p)if(substr($s,$-[0],1)=~/[A-Z]/);print$p;}
```
[Try it online!](https://tio.run/##Dcm9CsIwFEDhV7niRRJojQ5OIUNFHEQQwclSitbYBmt7SRrrD/rqscv5hkPa1oswHtEgxE1ApzCXToAQpcRc1QXDnMu@MrVmgk25KB3/ICmcyKF@2MTNlTl/dp1l6CKM01kWzbn6iTSJj5ngkqxpOiT5DSGBvTfFDZa27RtYt0/Y@DvpC@we2sKh0rA9vV@wass/ "Perl 5 – Try It Online")
Explanation:
1. Make copy of input string.
2. Remove all spaceses and transform string to lower case.
3. Then start loop over each letter. Test letter in same position in saved string for upper case. If it upper - make current letter captitalized.
Print letter.
Note that perl need to be run with "-n" command line switch
[Answer]
# [Python 3](https://docs.python.org/3/), 117 114 98 bytes
```
s=input()
i,*y=0,*s.replace(' ','').lower()
for c in y:print(end=[c,c.upper()]['@'<s[i]<'[']);i+=1
```
[Try It Online!](https://tio.run/##FcqxCsIwEADQ3a@47doaiuKmBlTEQQQR3EKHkp42WJMjbazx56O@@XEcWmcXKfXSWA5Dlk@MKKKciaIvPXFXa8oQUCDmZedG8r9xcx40GAtxyd7YISPbSKWFLgPzf1QKN7julanWqLDKV2Yq5ylt4RKMfsDOu9HCwb3hGJ5MDZxf5OHaEpzqT4S9u38B)
**Edit:** 3 bytes saved by UndoneStudios.
**Edit:** 14 bytes saved by Unrelated String.
This is pretty much my first code golf, so it's likely to be bad, minus help from comments below!
P.S. Yes, it's dumb that defining and incrementing `i` saves bytes over range(len(y)). Oh well.
---
# Python 3, 85 bytes
```
lambda s:''.join([c,c.upper()]['@'<b<'[']for b,c in zip(s,s.replace(' ','').lower()))
```
[Try It Online!](https://tio.run/##FctBC4IwFADgv/Jub4OxS5cIg4rwEEEE3czDnDNXuveYmumft/ruH099TWG15rhU2/vSmLYoDXQbRP0kH0RmldUDs4tC5hnuMCkSzDCvKEKhLPgAs2fRqU5Hx42xTiCgQpS6ofG/pFw4@tCLSuAeroO3LzhEGgOk9IHT0LIr4fJ2EW61g7OZJzjSA3/tCw)
This solution is entirely due to att; I'm keeping it separate because the strategy seems to me to have changed enough.
[Answer]
# [Java (JDK)](http://jdk.java.net/), 84 bytes
```
s->{int j=0,c;for(var k:s)if((c=k&95)>0)System.out.printf("%c",c^(s[j++]|~c/2)&32);}
```
[Try it online!](https://tio.run/##bVJdb9owFH3vrziJtC5RQ1Z12sOaAaJAvwstSddpjEnGSSAJsVnspKUd@@vMgVCqaVe2LJ3rc33uPY5JQWqxn6zm@XgWUdAZEQI3JGJ42QOEJFKhsbpl5zKa2WHOqIw4s9uciTwNsi90SrLhqIEQdaxErfESMYm4fmhRJ@SZUZAMybEwo9AwaD3Z//zJbBya7kLIILV5Lu15pgihob@jukV/GmIYHxyMfv@hH47M/Y9HprNcQYWjxKhVqaxkFTzykSqthitVlclwBGKWsoFXQAZCCiVtAwP6eaTBU6CmW1uohbs8oglOMv7IcMqfcJmn88BHvwgyeNMA1@R5gQ6f7DhlXfUCvK7rrc/AlRe9M2zxt3mF74gVYNs2tlvklAZCaOjeD7pXrWazqWmaamwxDiz4nL2XFnr9MnZlukwqbVXctlz3oT/ooLaNt80RnGCMNig68NFFgFPl1hkmOMcUF4hwiRhXSHCNGW6QogeGPjhuMccdfmGADC4EPEjcI8dXFHjAI77hCd/xrK/fWjrrozR9M/31DI43DpivBvxr/YwZ5Q3TqfKhTdQ05nKN2pK31f9qZRlZGKbp/Ies/2B6RV7ulXu5@gs "Java (JDK) – Try It Online")
# Credits :
* Dennis for the bit twiddling as it's a good way to shorten the answer
* Fhuvi for trimming 12 bytes.
[Answer]
# [MATL](https://github.com/lmendo/MATL), 18 bytes
```
kXz"@GX@)tk<?Xk]&h
```
Same approach as [Riley's 05AB1E answer](https://codegolf.stackexchange.com/a/128938/36398).
[Try it online!](https://tio.run/##y00syfn/PzuiSsnBPcJBsyTbxj4iO1Yt4/9/dUeFwNLM5GwFp6L88jwFt/wKBa/S3ILUFAX/stQihZCMVAWfxKpKBZf8dHUA "MATL – Try It Online")
[Answer]
# [C# (.NET Core)](https://www.microsoft.com/net/core/platform), 108 101 bytes
```
using System.Linq;s=>s.Replace(" ","").Select((c,i)=>s[i]>64&s[i]<91?char.ToUpper(c):char.ToLower(c))
```
[Try it online!](https://tio.run/##pVBNSwMxEL3vrxj2IAm0AUEEbbtS@0Wl2tpd6UE8xHTaBrfJmmRbq/jb12y7BREv4rsM8x5v5s0IWxfaYJFbqZYQ76zDNRtJ9doIApFya2ESfATgYR13UsBGyznccqkI3dMHsURl7udKNK0zfl6tojo6TVE4qZVlA1RopGDDnsrXaPhzik2x4iaKYAEtKGwrsmyKWcoFkhDCWhhSFmPpJ0TUJPX6o3yKzs9Oytq8OL0q7SzRD1mGhgh6WfUjvd33tPC3/AjZ8VF0imxmpEN/LhKFWzikJgsStuE@l@IFro3eKujrN7jJ1xnOYbxBA8kKYcTfd9DVS58u0W1j@I5QSht/XeTQuvLzSS9O9hVjN7wbwJH/rnv@n9t6yvn4FSbtOJ6Np12oH/Hb9M/gs/gC "C# (.NET Core) – Try It Online")
* 7 bytes saved after realizing that the `char` class has static `ToUpper()` and `ToLower()` methods.
[Answer]
# [Charcoal](https://github.com/somebody1234/Charcoal), 33 bytes
```
Ａ⁰χＦＬθ¿⁼§θι Ａ⁺¹χχ¿№α§θ⁻ιχ↥§θι↧§θι
```
[Try it online!](https://tio.run/##S85ILErOT8z5/37PQkeFwNLM5GwFp6L88jwFt/wKBa/S3ILUFAX/stQihZCMVAWfxKpKBZf89HM7QOofNW443/5@z7L3e9ac23Fo/6PGPYeWn9txbqcCWG7XoZ3n28@3A8Vbpp3bCJJ51Lj7HFDsUdtSiLpHbcshjP//AQ "Charcoal – Try It Online")
As I still don't know how to pass a string with whitespaces as a single input parameter into Charcoal code, I just assign in the header the test string to the Charcoal variable that represents what would be the first input (`θ`):
```
ＡA Quick Brown Fox Jumped Over The Lazy Dogθ
```
So the code has the same number of bytes as if the string were passed as first input.
You can see [here the verbose version](https://tio.run/##bY3BisIwFEX3fsXFjS8QQddd1VFBqcwI4weENLbBmrRJY3V@PraiyIBve885T5bCSSuqmHqvC0PjFPug5QkLZzuDtb1iG861yvF9UQ6/pUIm/m5Y2mLM0bDkJc64ZMnoaB0oU6ZoS2oYGwH6CFo1QVSe0nZjcnWlhmvGJ5g8duAZSPOc5n2EYwgNw6B@2WBaEvzt7rQJnnRP9ocHCPw43WOHulZOCq/@vWLP3IvKbPeZijFOL3Hqqzs) of the code.
[Answer]
# PHP, 181 bytes
I try get the minor numbers of bytes,this is my code:
```
<?php
$s=readline();
preg_match_all('/[A-Z]/',$s,$m,PREG_OFFSET_CAPTURE);
$s=strtolower(str_replace(' ','',$s));
while($d=each($m[0]))$s[$d[1][1]]=strtoupper($s[$d[1][1]]);
echo $s;
```
[Try it online!](https://tio.run/##TYzLasMwFET3@Yq7EEgGh6TrNBQ3sQOlkEfdTY0RQr5EJnJ0kew67c@7KtkEZjEMcw4ZmqbnFzI0Y2HtUTW2vaJIVjPyeJad6rWRylrBF1U2/6oXPGUhZV16OOU7uS@Kj7yUm@xQfp7yCEVH6H3vrBvRi1ilR7JKo@DAU/4PJ/E2mtaiYM0alTaCddWyThIWKtZUT3VMfbcMRNHyuEcWtXHAwmqaMjgOrb7Aq3fjFQp3g7ehI2xg/40eSoPwrn5/YOvOfw)
[Answer]
# Common Lisp, 104 bytes
```
(defun f(s)(map'string(lambda(x y)(if(upper-case-p x)(char-upcase y)(char-downcase y)))s(remove #\  s)))
```
[Try it online!](https://tio.run/##LYxBCsIwFESvMtSFP4seQhEXIojg0k1MftqgaT9JU1svX1NxOY95z7x8kmUhyy53cJQUBS3bNETfNfTS4WE1TZgVeUdZhGNtdOJaMCkyrY51lhWsj9@0/bv7A6USRQ79yNjcgVTAQlLCA8ih2uGavXliH4uCYz/hlIOwxWXkiFvLOOvPjEPfVEX8Ag)
Unusually short for the wordy Common Lisp!
Straightforward code:
```
(defun f (s)                     ; receive the string as parameter
  (map 'string                   ; map the following function of two arguments
       (lambda (x y)             ; x from the original string, y from the string with removed spaces
         (if (upper-case-p x)    ; if x is uppercase
             (char-upcase y)     ; get y uppercase
             (char-downcase y))) ; else get y lowercase
       s
       (remove #\  s)))
```
[Answer]
# Google Sheets, 213 bytes
```
=ArrayFormula(JOIN("",IF(REGEXMATCH(MID(A1,ROW(OFFSET(A1,0,0,LEN(A1))),1),"[A-Z]"),MID(UPPER(SUBSTITUTE(A1," ","")),ROW(OFFSET(A1,0,0,LEN(A1))),1),MID(LOWER(SUBSTITUTE(A1," ","")),ROW(OFFSET(A1,0,0,LEN(A1))),1))))
```
Input is in cell `A1` and the formula breaks down like this:
* `ArrayFormula()` lets us evaluate each term of `ROW()` independently
* `JOIN()` concatenates all those independent results into a single string
* `IF(REGEXMATCH(),UPPER(),LOWER()` is what makes it alternate using upper or lower case depending on what the case was at that position in the input
* `ROW(OFFSET())` returns an array of values `1` to `A1.length` that can be fed into the `MID()` function so we can evaluate each character in turn
Results of test cases: (It's easier to read if you click though to the larger version.)
[![TestCases](https://i.stack.imgur.com/zeGyF.png)](https://i.stack.imgur.com/zeGyF.png)
[Answer]
# [Ruby](https://www.ruby-lang.org/), 80 bytes
```
->a{n=a.downcase.delete' '
n.size.times{|i|(?A..?Z)===a[i]&&n[i]=n[i].upcase}
n}
```
[Try it online!](https://tio.run/##TYzLDoIwFET3fMVdUV3YP6gE4yMaozGwkrCocIONUghtVV7fXsGExM1MciZnKnOrrYPMLpa8lYzTtHjLhCukKT5RIwHiSKpEg1SLHFXbiW7m@ZR61zljjEcidl05JBuDmnJ0e0f2tjRaAUbEh4sRyQNW1fAM2@IDB5OXmML5hRWEd4Qjb2pYFxmJnUnSqLSQGYSbIPw1Bnp/2sHE//eBk9h@AQ "Ruby – Try It Online")
[Answer]
# Perl, 92 bytes
```
$p[$i++]=$-[0]while s/[A-Z]/lc($&)/e;s/\s//g;for$c(@p){substr($_,$c,1)=~tr[a-z][A-Z]};print;
```
**Explanation:**
```
$p[$i++]=$-[0]while s/[A-Z]/lc($&)/e;   #get locations of caps into an array at the same time converting letters to lowercase
s/\s//g;   #delete all spaces
for$c(@p){substr($_,$c,1)=~tr[a-z][A-Z]};   #convert lowercase letters to uppercase where uppercase letters were present
print;   # print (of course) :)
```
[Answer]
# [Perl 5](https://www.perl.org/), 40 bytes
37 bytes of code + `-F` flag. (note that on old versions of Perl, you might need to add `-an` flags)
```
print$F[$i++]=~/[A-Z]/?uc:lc for/\S/g
```
[Try it online!](https://tio.run/##K0gtyjH9r6xYAKQVdN3@FxRl5pWouEWrZGprx9rW6Uc76kbF6tuXJlvlJCuk5RfpxwTrp///76gQWJqZnK3gVJRfnqfgll@h4FWaW5CaouBfllqkEJKRquCTWFWp4JKfDgA "Perl 5 – Try It Online")
Explanations:  
Thanks to `-F`, `@F` contains a list of every characters of the input.  
`for/\S/g` iterates over every non-space character of the input. We use `$i` to count at which iteration we are. If `$F[$i++]` is an uppercase character (`/[A-Z]/`), then we print the uppercase current character (`uc`), otherwise, we print it lowercase (`lc`). Note that `uc` and `lc` return their argument unchanged if it isn't a letter.
---
Previous version (less golfed: 47 bytes): 
```
 s/ //g;s%.%$_=$&;$F[$i++]=~/[A-Z]/?uc:lc%ge
```
[Try it online!](https://tio.run/##K0gtyjH9r6xYAKQVdAvc/hfrK@jrp1sXq@qpqsTbqqhZq7hFq2Rqa8fa1ulHO@pGxerblyZb5SSrpqf@/@@oEFiamZyt4FSUX56n4JZfoeBVmluQmqLgX5ZapBCSkargk1hVqeCSnw4A "Perl 5 – Try It Online")
[Answer]
# [Charcoal](https://github.com/somebody1234/Charcoal), 18 bytes
```
⪫Ｅ⪫⪪θ ω⎇№α§θκ↥ι↧ιω
```
[Try it online!](https://tio.run/##PYxNCsIwEEb3nmLoKoF6AldVESwVFesBhnSwoTUTx/56@RgVXHzwHjw@U6MYxjaEk1jXqZytUwf0P7j41nbqkUICiU5hjCtJHMqsNtzHHFPIur2raPpUjY7B1XsSg09SNlrB49/090KvQsjg3FvTwFp4dLDjCfL@7qmC40ACZU1Q4GuGLd8WYTm0bw "Charcoal – Try It Online") Link is to verbose version of code. Explanation:
```
    θ               Input string
   ⪪                Split on spaces
  ⪫   ω             Join together
 Ｅ                  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 `⪫Ｅ`...`ω`.
[Answer]
# [GNU sed](https://www.gnu.org/software/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!](https://tio.run/##TY9Za8JQFITf76/4AoUqqBf7aB4k1rjXLbEWmz643GqqJjaL249vGq1CBw5zzgwDc0K1SJKSIUL5IHUpSumSeTfyk4@snimUs1J3Nk7RiZ0nKaKL51hZPRWLfye6lEIiZ0aSNFwNW4WRJgwGsTtfUwn8g0fNP9KKtzu1oLdXAfZK0ZmeT1T9pYjSgOstsU3LvrKyoma3zl3/76e6uHGhUOA@YTyfqzDUMEdDs22Uy2VN09hOTzOVY@F7j1GObu8CYXpRWuCGvmFZ496wSv6OtPiUCjOemVNlgYmixid1ljRY0cSlxRdt1nTY8MKWLh49fPrsGPDNkACLEJuIETGv7Blz4I0jE86C6y8//i5yfS9M8sEv "sed 4.2.2 – 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
```
[Answer]
# [Rust](https://www.rust-lang.org/), 143 bytes
```
|s|str::to_lowercase(s).replace(' ',"").chars().zip(s.chars()).for_each(|(c,d)|print!("{}",if d.is_uppercase(){c.to_ascii_uppercase()}else{c}))
```
[Try it online!](https://tio.run/##fVPbTuMwEH3PV5z2ARKpBO1rVl1UINyhQNuF5QWliUMNqR1sp/d@OztOGygIMVI8zsyZM5dMVKHNW170kQoMIy5cDzt/QOfcAUnGDEYBOd0tbZSHJrC7CzPNGSIhpIkMlwJSZFMIFsdM60hNwQXMgEFmCW6JHyOmtMUVmiXoT0FBvirE20IviDUIjHzM5JipONLM1Z6vWJ5FMXO3sd2o1z0/HkRKu54/47mrqzfPT6V6ZFE8cBdu3Ei8Ra64MDW3Pl/WGzxF4nP9WOT5mtebxz4linTM@aZ5yTLN5vHS895@v/esTUJNNK0OAi6DoDS43gdiWBjqM6eziY6hzE9BINj4K6RfpClT32PGA56xEtl@cX/5frPMVmg@Y0Fw2br3VgVwQQOJkseMC@ZufZBW38gKtVsWY1tmw9xM3U1viVjXuor140wS27oQK8v3m52HJVwjqe4hzf5nYuJjkfqB73NM9aVObWxgLfNlOMlZbFgS0L1dmLWj3sBWmYH0uu3fn6hG7sr/xVxmyATlqJPnm6re57BR99JZvp3wGrpMm5pDNxPai9PCTcHjF@wrORY4khOcFcOcdrlNm40urfpFNJviUD45rdceIfuqPRapnFhYmEhCmROWRbN/CUEcQ/S0C@iGnW6pWcecXh2jsm/6yV7hjX0lZdHiuBuujOxDEdRZx/i@j@rRRflj1hD2bsPz1t7eXq1Wo5992mcNJFJs02yv2lY2oldShbJCsSpyFbiKE9KK44TC0CDWct3qdO7at4fYqWTlzyOtx1IlH2YabIR99HGAGIdIEILhCCmO8YQTDHAKjjM84xwvuECGSwxxBYE2JK6R4wavuIVCBxpdGPRQ4C9GuMMY95jgATOnFfX7B3GShCxNj58Gg1P@/Hz@kmWXQyHaMs9vXpXqaGN6xWh0N55MHmZ2XewkaPB0/Ac "Rust – Try It Online")
Ungolfed:
```
  |input: &str|
    input.to_lowercase()          // create lowercase string
         .replace(' ', "")        // remove all spaces
         .chars()                 // iterate over characters
         .zip(input.chars())      // zip with input character iterator
         .for_each(|(c, input_c)| // for each character in both strings:
           print!("{}",                  // print the following:
             if input_c.is_uppercase() { // if the input character is uppercase
               c.to_ascii_uppercase()    // `c` in uppercase
             }
             else { c }                  // else `c`
           )
         )
```
[Answer]
# [Go](https://go.dev), 184 bytes
```
import(."strings";u"unicode")
func f(s string)string{S:=[]rune(ReplaceAll(ToLower(s)," ",""))
for i:=range s{if i<len(S)&&u.IsUpper(rune(s[i])){S[i]=u.ToUpper(S[i])}}
return string(S)}
```
[Attempt This Online!](https://ato.pxeger.com/run?1=VVPbbptAEH3oG19xjJQUJEJfK6c0cmLiOBc7Mbhpc1GFYbGJ8S6GxXaM_CV9iSr1H9pPab-mA9hKOxI6y5k5s8Pu4dv3sXj5lXj-1BszzLyIK9EsEak01XAm1R-5DA_e__5Zc5qpZjKN-DhTD3M155EvAqbqSphzH6GWoc7qNRRO07p_THPOtAFLYs9nrTjWXHEplizVMt1QoRqqqpNepIiaVupxmiErohDRh5hxzdH393Ozmw2ThBRVp-w-etT1wiGwctMVdap81TcbJWUyT_l2DJJv6vn_vHknnxMGt8zkvkQR8cQAWyXb0o1Sf0P5_ZqOQlEky2SGpoX7R7dQCvUsasAlrkEj01ra5XJjUKaFmzzypzhOxZLjVKxwns8SFqC_YCncCcOlt35GW4xJ2poPqXaU9pc8FKuy0A4E1ckzFnvrLwEVVU3L7WkuuLbjVsgc2e11sOP_zRNPnbeMLAmCsp53XLsm2StQcbXDVmmaJnZPlvs-y7IG7OHAvmgdHR01Gg06k-cRMxAI_lYa6PXLoP1e9XXsxCxP2U5bS2slF2XUe9tc0tFs47rlOLf9QRsHu6DuVUXiZdlSpMFrYnvgHo4xwgl8tBHABsMpQnQwxhkm6CLCOZ5wgSkuEeMKM_TA0YfANRLcYI4BUjjIyBESQ-T4hAVuscRnrHCHdXlT3mh04geBzcKwM55MutHT08U0jq9mnPdFktzM09TJpBzmi8XtcrW6W9fTobqT6kIIiCJvXZPHZMw11bIsOG5r4IJWam38r4YsfVabv7ZdoXh-RYaaNMmqukK_hJg2LWmSZy2LsocNMUVRNQ41dW_-wGF9RIlahTr25ANXDVQNDJCkXJPcoE76hsb6byrYvTa2UynK9r95eanxLw)
[Answer]
# [Noulith](https://github.com/betaveros/noulith), 88 bytes
```
\s->(for(p<<-s.lower filter (!=' '))yield if(s[p[0]].is_upper)p[1].upper else p[1])join""
```
[Try it online!](https://betaveros.github.io/noulith/#Zjo9Cgpccy0+KGZvcihwPDwtcy5sb3dlciBmaWx0ZXIgKCE9JyAnKSkgeWllbGQgaWYoc1twWzBdXS5pc191cHBlcilwWzFdLnVwcGVyIGVsc2UgcFsxXSlqb2luIiIKCjsKCnN0cnVjdCBUIChpbnAsd2FudCk7Cgp0cyA6PSBbClQoIkhpISBUZXN0ISIsIkhpIXRFc3QhIiksClQoIkEgUXVpY2sgQnJvd24gRm94IEp1bXBlZCBPdmVyIFRoZSBMYXp5IERvZyIsIkFxVWlja2JyT3duZm94SnVtcEVkb3ZlcnRIZWxhellkb2ciKSwKVCgidGVzdGluZyBURVNUaW5nIFRlU3RJTkcgdGVzdGluZyB0ZXN0aW5nIFRFU1RJTkciLCJ0ZXN0aW5ndEVTVEluZ3RlU3RJbkdURXN0aW5ndGVzdGluZ3Rlc3RpTkciKSwKVCgiVEVTVElORy4uLiAuLi4gLi4uIHN1Y2Nlc3MhIEVVUkVLQT8/PyEhISBtYXliZSwgZG9uJ3QsIE5PT09PTyIsIlRFU1RJTkcuLi4uLi4uLi5zdWNjZXNzIWV1cmVLQT8/PyEhIW1heWJlLGRvbid0LG5vb29vbyIpLApUKCJFbnRlciAgICAgICAgUEFTU1dPUkQgLS0tLS0tLS0tIiwiRW50ZXJwYXNzd29yZC0tLS0tLS0tLSIpLApUKCJBIGEgQiBiIEMgYyBEIGQgRSBlIEYgZiBHIGcgSCBoIEkgaSBKIGogSyBrIEwgbCBNIG0gTiBuIE8gbyBQIHAgUSBxIFIgciBTIHMgVCB0IFUgdSBWIHYgVyB3IFggeCBaIHoiLCJBYWJiQ2NkZEVlZmZHZ2hoSWlqaktrbGxNbW5uT29wcFFxcnJTc3R0VXV2dld3eHhaeiIpLApUKCIgIFRFU1QiLCJ0ZVNUIiksCl07Cgpmb3IgKHQgPC0gdHMpICgKYSA6PSBmICh0LmlucCk7CmlmIChhIT10LndhbnQpIHByaW50KHQuaW5wLCJcbiIsYSwiXG4iLHQud2FudCwiXG4iLHQud2FudD09YSwiXG4iKTsKKTs=)
[Answer]
# C, 103 bytes
```
i,j,c;f(char*s){for(i=j=0;c=tolower(s[j++]);)c-32&&putchar(c-32*(s[i]>64&&s[i]<91&&c>96&&c<123))&&++i;}
```
[Try it online!](https://tio.run/##bZBZT8JAFIXf/RVfeaitLHELialIUOqKoLQu0fiAwwCD0GI7FZf423FqJPGBk9xl7rn3zMwV5aEQi4UqjUvCGzhi1Es2UvdrECeOqo1rm56o6XgSz2XipI/jYvHJ9VxR3tm27Vmm824nP20YUj0dVHdtO0/297ZsWxzsVY3f39recV3bLhaV971QkWbaU5Hjrn2tYTBwCqfKIpSptgquZ0RTp2CSJdngOlPihcMknkccx@@cZ9OZ7NN5kwnhSNLqfX7QjIerprWRVdGQ0A/C3ygDfdY@YVn/z5v6Kok/qlKpsLQ0E0KmqYV/0/UvGvV63bIs862PZ1miH0frukS7k2OVoB9p8/I/XDWC4K7TbVJeYvUSehzyzBGCJn18JMcMOGHIKSPOUJwz5oIXWky4ZEqbiA4xV8y45pUuCQEpIZobMm55444597zzwGd@2/fiBw)
]