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
43 Answers 43
C (gcc), (削除) 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;}
Jelly, (削除) 14 (削除ここまで) 13 bytes
nŒlTɓḲFŒlŒuṛ¦
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.
Python 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))])
Equivalently:
Python 2, 114 bytes
lambda x:''.join([[str.lower,str.upper][x[i].isupper()](x.replace(' ','')[i])for i in range(len(x)-x.count(' '))])
-
\$\begingroup\$
''.join([(X[i].lower,X[i].upper)[x[i].isupper()]()for i in range(len(X))])for -5 bytes. \$\endgroup\$ovs– ovs2017年06月29日 12:08:49 +00:00Commented Jun 29, 2017 at 12:08 -
\$\begingroup\$ 96 bytes:
x=input();print''.join([(c+c).title()[1-x[i].isupper()]for i,c in enumerate(x.replace(' ',''))])\$\endgroup\$movatica– movatica2024年01月30日 10:58:28 +00:00Commented Jan 30, 2024 at 10:58
Python 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!
-
\$\begingroup\$ Can you just compare
sinstead ofs[0]? \$\endgroup\$xnor– xnor2017年06月29日 04:33:01 +00:00Commented Jun 29, 2017 at 4:33 -
\$\begingroup\$ Yes, of course. Thanks! \$\endgroup\$Dennis– Dennis2017年06月29日 04:35:07 +00:00Commented Jun 29, 2017 at 4:35
-
2\$\begingroup\$
(c*2).title()can get you both cases, though switched. \$\endgroup\$xnor– xnor2017年06月29日 04:52:53 +00:00Commented Jun 29, 2017 at 4:52 -
\$\begingroup\$ Another 3 bytes. Thanks again! \$\endgroup\$Dennis– Dennis2017年06月29日 05:04:08 +00:00Commented Jun 29, 2017 at 5:04
-
\$\begingroup\$ Tricky! Took me a while to figure out that
c>' '!=f()is equivalent to(c>' ') and (' '!=f()). \$\endgroup\$Chas Brown– Chas Brown2017年06月29日 07:41:46 +00:00Commented Jun 29, 2017 at 7:41
05AB1E, (削除) 15 (削除ここまで) 14 bytes
-1 byte thanks to Emigna
ðKuvy1Nè.lil}?
ðK # Remove spaces
u # Convert to uppercase
vy # For each character...
1Nè # 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
-
\$\begingroup\$ You save a byte if you uppercase the space-removed string and lowercase it in the condition. \$\endgroup\$Emigna– Emigna2017年06月29日 07:35:51 +00:00Commented Jun 29, 2017 at 7:35
Haskell, (削除) 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)
-
\$\begingroup\$ On mobile, but looks like you can save some bytes with filter(/=' ') \$\endgroup\$Henry– Henry2017年06月29日 03:29:02 +00:00Commented Jun 29, 2017 at 3:29
-
\$\begingroup\$ Yep, certainly can. Missed the part of the spec noting that spaces were the only whitespace that needed removing. \$\endgroup\$Julian Wolf– Julian Wolf2017年06月29日 04:43:00 +00:00Commented Jun 29, 2017 at 4:43
-
1\$\begingroup\$
filter(>' ')for one byte less \$\endgroup\$nimi– nimi2017年06月29日 05:33:41 +00:00Commented Jun 29, 2017 at 5:33 -
2\$\begingroup\$ I think the body of the lambda can be shortened to
last(toLower:[toUpper|isUpper p])c\$\endgroup\$Zgarb– Zgarb2017年06月29日 06:39:25 +00:00Commented Jun 29, 2017 at 6:39 -
\$\begingroup\$ Switching the arguments of
zipWithshould save one more byte:f s=zipWith(\p->last$toLower:[toUpper|isUpper p])s$filter(>' ')s. \$\endgroup\$Laikoni– Laikoni2017年06月29日 07:07:26 +00:00Commented Jun 29, 2017 at 7:07
V, 24 bytes
ÄVuÓó
ejlDò/2円lõ
vuk~òGd
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
2円l " 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
-
\$\begingroup\$ @DLosc Good questions! The newlines signal the end of a regex command, such as a substitute (remove) or search command. More detail is on this page: github.com/DJMcMayhem/V/wiki/Regexes \$\endgroup\$DJMcMayhem– DJMcMayhem2017年06月29日 19:13:25 +00:00Commented Jun 29, 2017 at 19:13
Python 2, 100 bytes
s=input()
print"".join([c.lower(),c.upper()][s[i].isupper()]for i,c in enumerate(s.replace(" ","")))
-
3\$\begingroup\$ Welcome to PPCG, and very good first answer! \$\endgroup\$ETHproductions– ETHproductions2017年06月29日 19:15:15 +00:00Commented Jun 29, 2017 at 19:15
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
-
\$\begingroup\$ Ninjad :P codegolf.stackexchange.com/a/128951/65836 \$\endgroup\$Stephen– Stephen2017年06月28日 22:22:19 +00:00Commented Jun 28, 2017 at 22:22
-
\$\begingroup\$ Could you do
'@'<s[i]&s[i]<'['?? \$\endgroup\$ETHproductions– ETHproductions2017年06月28日 23:42:25 +00:00Commented Jun 28, 2017 at 23:42 -
\$\begingroup\$ @StepHen: Aw, man, didn't see that last night while I was working on this. \$\endgroup\$Shaggy– Shaggy2017年06月29日 07:38:32 +00:00Commented Jun 29, 2017 at 7:38
-
\$\begingroup\$ @ETHproductions: I was wondering if that might be shorter but I was too lazy to look up which characters I'd need to use :D Turns out it doea save a byte; thanks. \$\endgroup\$Shaggy– Shaggy2017年06月29日 07:40:07 +00:00Commented Jun 29, 2017 at 7:40
-
R, (削除) 96 (削除ここまで) 95 bytes
\(s,p=utf8ToInt(s)){p[U]=p[U<-p%%65<26]+32
r=p[p>32]
r[V]=r[V<-U[seq(r)]&r>96]-32
intToUtf8(r)}
Alternative
\(s,`?`=utf8ToInt,r=?tolower(s)){r=r[r>32]
r[V]=r[V<-(?s)[seq(r)]%%65<26&r>96]-32
intToUtf8(r)}
Based on int 21h -- Glory to Ukraine --'s R answer, but operates on character codes instead of strings.
-
\$\begingroup\$ Nice one! I have tried with
utf8ToInttoo but have failed because of the punctuation \$\endgroup\$Glory2Ukraine– Glory2Ukraine2024年06月03日 13:37:42 +00:00Commented Jun 3, 2024 at 13:37
Alice, 32 bytes
/..- ~l+u~mSloy
\ia''-y.'Qa.+a@/
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
Retina, (削除) 77 (削除ここまで) 71 bytes
.+
$&¶$&
T`L `l_`.+$
+`((.)*)[A-Z].*(¶(?<-2>.)*)
1ドル3ドル
.+¶
T`l `L_` .?
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.
-
\$\begingroup\$ Small question: Are the
(.?)and4ドルparts needed? It looks like having an optional group at the end does nothing. \$\endgroup\$Kobi– Kobi2017年06月29日 11:38:40 +00:00Commented Jun 29, 2017 at 11:38 -
\$\begingroup\$ @Kobi Nothing small about that question! It had originally been part of an attempt at using lookarounds to match the characters to be uppercased directly instead of having to translate them as a separate step. \$\endgroup\$Neil– Neil2017年06月29日 12:26:10 +00:00Commented Jun 29, 2017 at 12:26
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:
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
-
1\$\begingroup\$ Take a
char[]instead of a String for this one, you'll save up plenty of bytes! \$\endgroup\$Olivier Grégoire– Olivier Grégoire2017年06月30日 07:57:34 +00:00Commented Jun 30, 2017 at 7:57 -
\$\begingroup\$ On the other hand, I answered too with another algorithm. And here, I take the opposite arguments: in =
String, out =char[]:-) \$\endgroup\$Olivier Grégoire– Olivier Grégoire2017年06月30日 08:39:15 +00:00Commented Jun 30, 2017 at 8:39
Perl 5, 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
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
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;}
Explanation:
Make copy of input string.
Remove all spaceses and transform string to lower case.
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
-
-
1\$\begingroup\$ I think that you need to count
+1byte for the-nflag. Other than that, this looks good! Welcome to the site! :) \$\endgroup\$DJMcMayhem– DJMcMayhem2017年07月03日 18:25:58 +00:00Commented Jul 3, 2017 at 18:25 -
\$\begingroup\$ @StepHen it's Perl 5, colud you add link? I failed to run my code there in propper way. \$\endgroup\$Weizel– Weizel2017年07月04日 17:44:30 +00:00Commented Jul 4, 2017 at 17:44
-
\$\begingroup\$ Happy to see a new Perl golfer! I've added the TIO link to your answer and improved the formatting. \$\endgroup\$Dada– Dada2017年07月04日 18:06:27 +00:00Commented Jul 4, 2017 at 18:06
Charcoal, 18 bytes
⪫E⪫⪪θ ω⎇Noα§θκ↥ι↧ιω
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
Noα 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:
⭆⪫⪪θ ω⎇Noα§θκ↥ι↧ι
⭆ is roughly equivalent to ⪫E...ω.
-
\$\begingroup\$ Save two more bytes by replacing
⪫⪪θ ωwith−θ. \$\endgroup\$Neil– Neil2024年06月04日 16:45:21 +00:00Commented Jun 4, 2024 at 16:45
Python 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
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()))
This solution is entirely due to att; I'm keeping it separate because the strategy seems to me to have changed enough.
-
1\$\begingroup\$ Welcome to PPCG! Nice first submission! However, complying to our site's I/O standards, your submission must either be a function of a string or take input; you cannot assume input is in a variable. Hope you enjoy your stay! :) \$\endgroup\$2017年06月28日 21:29:43 +00:00Commented Jun 28, 2017 at 21:29
-
\$\begingroup\$ Thanks; edited a function in, but also saved 5 bytes in the body :D \$\endgroup\$Luke Sawczak– Luke Sawczak2017年06月28日 21:35:47 +00:00Commented Jun 28, 2017 at 21:35
-
1\$\begingroup\$ @LukeSawczak save a ton of bytes by changing to one space for indentation, and maybe add a Try It Online! link if you want to \$\endgroup\$Stephen– Stephen2017年06月28日 21:37:08 +00:00Commented Jun 28, 2017 at 21:37
-
1\$\begingroup\$ You can remove the space after
return. \$\endgroup\$CalculatorFeline– CalculatorFeline2017年06月28日 21:45:03 +00:00Commented Jun 28, 2017 at 21:45 -
2\$\begingroup\$ shaved it down a bit more \$\endgroup\$Unrelated String– Unrelated String2023年11月28日 01:55:02 +00:00Commented Nov 28, 2023 at 1:55
Go, 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)}
Noulith, 88 bytes
\s->(for(p<<-s.lower filter (!=' '))yield if(s[p[0]].is_upper)p[1].upper else p[1])join""
Java (JDK), 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);}
Credits :
- Dennis for the bit twiddling as it's a good way to shorten the answer
- Fhuvi for trimming 12 bytes.
-
\$\begingroup\$ @ceilingcat that doesn't work:
Hi! Test!should becomeHi!tEst!, but with your solution it becomesHi!Test. \$\endgroup\$Olivier Grégoire– Olivier Grégoire2019年11月10日 11:40:40 +00:00Commented Nov 10, 2019 at 11:40
Vyxal, 4 bytes
ȧ⇩$•
Helps to have a "with capitalisation of" built-in
Explained
ȧ⇩$•
ȧ # 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.
-
\$\begingroup\$ 14:
⍜▽¯≠1±:⌵▽≠@ ..\$\endgroup\$noodle person– noodle person2024年06月08日 02:40:37 +00:00Commented Jun 8, 2024 at 2:40
SM83, 25 bytes
Makes use of the fact that all the legit punctuation is less than @ and therefore has bit 6 turned off, so we can assume that if bit 6 is on it's a letter.
54 5D 1A 13 FE 20 28 FA 47 A6 CB 77 78 28 05 E6
20 CB AE B6 22 B7 20 EA C9
Disassembled:
f:
ld d,h // 54 // copy hl into de
ld e,l // 5D
l:
ld a,(de) // 1A // a = *(de++)
inc de // 13
cp a,' ' // FE 20 // if a == ' '
jr z,l // 28 FA // loop back
ld b,a // 47 // safeguard a
and (hl) // A6 // AND with *hl
bit 6,a // CB 77 // are both letters?
ld a,b // 78 // copy back
jr z,s // 28 05 // if so jump straight to store
and 20ドル // E6 20 // otherwise mask off all but 5th bit
res 5,(hl) // CB AE // and erase 5th bit of *hl
or (hl) // B6 // and or them together
s:
ld (hl+),a // 22 // *(hl++) = a
or a // B7 // cheap test for 0
jr nz,l // 20 EA // jump back if wasn't null
ret // C9 // return
R, (削除) 106 (削除ここまで) 101 bytes
-5 bytes thanks to Giuseppe
\(s,`+`=casefold,l=+gsub(" ","",s)){for(i in el(gregexpr("[A-Z]",s)))substr(l,i,i)=substr(l,i,i)+1;l}
Pretty close to my target <100 bytes:)
In this approach some code is reduced by using a single function casefold (with a short alias) instead of the usual tolower/toupper couple.
An attempt to remove the curly braces did not improve the byte count.
-
2\$\begingroup\$ Broken the 100 byte target: codegolf.stackexchange.com/a/273422/55372 \$\endgroup\$pajonk– pajonk2024年06月03日 12:16:12 +00:00Commented Jun 3, 2024 at 12:16
-
1
-
1\$\begingroup\$ using
casefoldis probably another few bytes. \$\endgroup\$Giuseppe– Giuseppe2024年06月04日 14:35:12 +00:00Commented Jun 4, 2024 at 14:35 -
1\$\begingroup\$ @Giuseppe thanks! this is totally different from my original answer though =) \$\endgroup\$Glory2Ukraine– Glory2Ukraine2024年06月04日 15:17:54 +00:00Commented Jun 4, 2024 at 15:17
-
1\$\begingroup\$ oh that
<-should be an=! \$\endgroup\$Giuseppe– Giuseppe2024年06月04日 15:55:07 +00:00Commented Jun 4, 2024 at 15:55
C# (.NET Core), (削除) 108 (削除ここまで) 101 bytes
using System.Linq;s=>s.Replace(" ","").Select((c,i)=>s[i]>64&s[i]<91?char.ToUpper(c):char.ToLower(c))
- 7 bytes saved after realizing that the
charclass has staticToUpper()andToLower()methods.
Charcoal, 33 bytes
A0χFLθ¿=§θι A+1χχ¿Noα§θ−ιχ↥§θι↧§θι
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 (θ):
AA 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 of the code.
-
1\$\begingroup\$ I said in another answer but just in case you forget, just input as a python array with one element \$\endgroup\$ASCII-only– ASCII-only2017年07月27日 02:38:52 +00:00Commented Jul 27, 2017 at 2:38
-
\$\begingroup\$ I just require the input to have a trailing newline. \$\endgroup\$Neil– Neil2017年12月01日 16:58:35 +00:00Commented Dec 1, 2017 at 16:58
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;
-
\$\begingroup\$ Instead of a Constant
PREG_OFFSET_CAPTUREyou can use the value256,$argnis a shorter variable asreadline()for an input and I thinkctype_upperand use oflcfirstanducfirstwill save lots of bytes with one loop and use of$$iand ternary operator \$\endgroup\$Jörg Hülsermann– Jörg Hülsermann2017年07月02日 22:24:15 +00:00Commented Jul 2, 2017 at 22:24
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)))
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)))
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 ofROW()independentlyJOIN()concatenates all those independent results into a single stringIF(REGEXMATCH(),UPPER(),LOWER()is what makes it alternate using upper or lower case depending on what the case was at that position in the inputROW(OFFSET())returns an array of values1toA1.lengththat can be fed into theMID()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.)
0, 2, 8, 14, 18, 23, 27, 32' They are0, 2, 8, 14, 18, 25, 30, 34, 39\$\endgroup\$