| Bytes | Lang | Time | Link |
|---|---|---|---|
| 059 | Zsh | 241107T050839Z | roblogic |
| 008 | APL Dyalog Extended | 241107T154520Z | Adá |
| 026 | QuadR | 241107T142219Z | Adá |
| 063 | AWK | 241106T205035Z | xrs |
| 128 | Casio BASIC casio fx9750GIII | 241019T183524Z | madeforl |
| 062 | JavaScript ES6 | 170525T172037Z | Arnauld |
| 008 | Uiua SBCS | 240725T130745Z | chunes |
| 055 | R | 240725T115837Z | int 21h |
| 103 | brev | 220818T124013Z | Sandra |
| 008 | Vyxal r | 220721T031907Z | naffetS |
| 029 | SM83 | 220721T020523Z | NoLonger |
| 098 | MUMPS | 220720T041856Z | Ryan Pat |
| 017 | Perl 5 p | 220718T060801Z | Sisyphus |
| 010 | Japt v2.0a0 | 191222T231007Z | Shaggy |
| 104 | Javascript | 200611T230832Z | Justin |
| 110 | Acc!! | 191223T083125Z | DLosc |
| 084 | C# Visual C# Interactive Compiler | 190227T090554Z | dana |
| 040 | J | 190227T050840Z | Jonah |
| 024 | Perl 5 | 170706T074148Z | Dom Hast |
| 095 | Python 3 | 170706T081011Z | Fedone |
| 192 | Python 3 | 170528T005552Z | user6927 |
| 079 | Groovy | 170613T181926Z | Magic Oc |
| 264 | Google Sheets | 170613T175340Z | Engineer |
| 038 | q/kdb+ | 170527T131015Z | mkst |
| 056 | C tcc | 170525T174649Z | cleblanc |
| 088 | Haskell | 170529T021058Z | Generic |
| 086 | PowerShell | 170527T210211Z | Matt |
| 041 | Ruby | 170525T204251Z | daniero |
| 018 | Alice | 170527T045603Z | Nitrodon |
| 033 | GNU Sed | 170525T173530Z | Digital |
| 100 | C# | 170526T111855Z | TheLetha |
| 063 | Python 3 | 170525T172834Z | Rod |
| 099 | Java 8 | 170526T074153Z | Kevin Cr |
| 054 | PHP 5 | 170526T024755Z | user6395 |
| 016 | Convex | 170525T231611Z | GamrCorp |
| 014 | Japt | 170525T175456Z | powelles |
| nan | 170525T214937Z | Brad Gil | |
| 013 | Jelly | 170525T172139Z | Dennis |
| 011 | Pyth | 170525T200552Z | FryAmThe |
| 032 | Retina | 170525T194819Z | FryAmThe |
| 064 | C | 170525T183821Z | user2301 |
| 024 | CJam | 170525T180514Z | Business |
| 015 | MATL | 170525T172714Z | Luis Men |
| 008 | 05AB1E | 170525T174539Z | Adnan |
| 071 | PHP | 170525T181554Z | Jör |
| 025 | Brachylog | 170525T180147Z | Fatalize |
| 013 | V | 170525T172423Z | DJMcMayh |
| 046 | Retina | 170525T174010Z | Neil |
Zsh, 64 59 bytes
for x (${(Us::)1}){${x/[A-Z]}&&((++n%2))&&x=$x:l;printf $x}
Here we split and uppercase (Us::) the argument $1, and iterate for x each character. If x is in [A-Z], and n is odd, then change x to lowercase. Finally, print x.
(++n%2 alternates between true/false for every letter.)
(please ignore garbage on stderr)
Related; but I couldn't get $MATCH[1] and $MATCH[2] to alternate properly
APL (Dyalog Extended), 8 bytes
⊢×≠\⍤|⍤×
⊢ the argument
× case-adjusted by (1: uppercase; 0: lowercase)
≠\⍤… the XOR-scan of…
|⍤… the absolute value of…
× the case (1: uppercase; −1: lowercase)
QuadR, 26 bytes
(\pL)(\PL*)(\pL?)
\u1\2\l3
Replace instances of "letter, 0 or more non-letters (greedy), optional letter" with the uppercase first letter, the non-letters, the lowercase last letter.
AWK, -F "" -vOFS="" 63 bytes
{for(;i++<NF;$i=x?tolower($i):toupper($i))$i~/[A-Za-z]/&&x=!x}1
Doesn't seem to work in TIO.
for(;i++<NF; # for each char
$i=x?tolower($i):toupper($i)) # switch by x
$i~/[A-Za-z]/ # only letters
&&x=!x # flip x
}1 # output default
Casio BASIC (casio fx-9750GIII), 128 bytes
?→Str 1
For 1→I To StrLen(Str 1
Isz H
StrSrc("ABCDEFGHIJKLMNOPQRSTUVWXYZ",StrMid(Str 1,I,1))=0⟹Dsz H
StrUpr(StrMid(Str 1,I,1))→Str 3
MOD(H,2⟹StrLwr(Str 3)→Str 3
Str 2+Str 3→Str 2
Next
Locate 1,1,Str 2
starts with lowercase
JavaScript (ES6), 66 63 62 bytes
Starts with uppercase.
s=>s.replace(/[a-z]/gi,c=>c[`to${s?'Upp':'Low'}erCase`](s=!s))
Uiua SBCS, 8 bytes
⍜▽¯\≠⊸±⌵
⍜▽¯\≠⊸±⌵
⌵ # uppercase
⊸± # letter mask
\≠ # scan by inequality
⍜▽¯ # swapcase according to mask
R, 60 55 bytes
\(s)gsub("([a-z][^a-z]*)([a-z]?)","\\U\\1\\L\\2",s,T,T)
Some explanation: This solution uses one single gsub and a regex which matches the pairs of letters: (1) the first letter followed by zero or more non-letter characters and (2) an optional second letter. Accordingly, the 1st catching group is capitalized and the 2nd one is converted to the lowercase. Two positional arguments stand for ignore.case = TRUE and perl = TRUE.
brev, 103 bytes
(define h(make-parameter #f))(over(if(char-alphabetic? x)((if(h(not(h)))char-upcase char-downcase)x)x))
Are the examples wrong? How do you get to InClUdE from AsCiI?
SM83, 29 bytes
Input pointer in hl, output in de, trashes bc, a, flags.
06 55 2A 4F CB AF D6 41
38 0C FE 1A 30 08 CB A9
CB 08 38 02 CB E9 79 12
13 B7 20 E6 C9
Disassembled:
fun:
ld b,$55 ;; 06 55 ; set b to alternating bits
loop:
ld a,(hl+) ;; 2A ; load char
ld c,a ;; 4F ; and into c
res 5,a ;; CB AF ; turn a-z into A-Z
sub 65 ;; D6 41 ; subtract 'A'
jr c,store ;; 38 0C ; if was less, not a letter
cp 26 ;; FE 1A ; compare to 26
jr nc,store ;; 30 08 ; if not less, not a letter
res 5,c ;; CB A9 ; it is a letter; make uppercase
rrc b ;; CB 08 ; rotate b and check low bit
jr c,store ;; 38 02 ; every other time skip next bit
set 5,c ;; CB E9 ; actually, make lowercase
store:
ld a,c ;; 79 ; store faster this way
ld (de),a ;; 12 ; store
inc de ;; 13 ; and increment
or a ;; B7 ; quick test against 0
jr nz,loop ;; 20 E6 ; if not 0 continue
ret ;; C9 ; else return
MUMPS, 98 bytes
f i=1:1:$l(s) s c=$e(s,i) s:c?1A u=c?1U,f=$s(f="":u,1:'f),$e(s,i)=$c(2*f-1*-'(f=u)*32+$a(c))
w s
f i=1:1:$l(s): Seti=1and loop over this line of code untilireaches the length of the input strings(inclusive).iis incremented by 1 each loop.s c=$e(s,i): Assign theith character ofstocusing the$extractfunction.s:c?1A: Execute the followingsetstatements ifcis an alphabetical character (uppercase or lowercase). This line and the next use M's built-in string matching system.u=c?1U: Makeua boolean storing whethercis an uppercase character. There is only one data type in M sou's value will be1for true or0for false.f=$s(f="":u,1:'f): Makefa boolean storing whethercshould be an uppercase character.f's value will also be1or0. It's assigneduif this is the first alphabetic character because the first alphabetic character is in the right case by definition. Otherwise its value is flipped from its previous value using thenotoperator'.$e(s,i)=$c(2*f-1*-'(f=u)*32+$a(c)): The left-hand side is theith character ofsagain; we're setting it equal to the right-hand side which modifiessitself.$a(for$ascii) and$c(for$char) are inverses: the former converts characters to the corresponding ASCII number and the latter converts numbers back into characters. The argument of$cis a compact way to add 32 to$a(c)ifcis lowercase but should be uppercase, subtract 32 from$a(c)ifcis uppercase but should be lowercase, and add 0 ifcis already in the right case. Note that M's operator precedence is strictly left to right, so2*f-1*xis unintuitively the same as(2*f-1)*x.Finally,
w sprints the now-alternatings.
Both lines have a leading space so that their first characters are interpreted as commands and not labels.
Javascript, 104 bytes
I'm kind of new to this but wanted to submit anyway.
x=>x.split('').forEach((s,i)=>x+=i%2?s.toUpperCase():s.toLowerCase())||x.substring(x.length/2,x.length);
It takes in a string, splits it into an array so it can loop through with forEach and fit in as a one-liner, then goes through each character and index and adds the uppercase or lowercase version of the character depending on if the index is odd or not, then "or"s it with what I want to return (since otherwise it's undefined) and returns the second half of the string, so I don't have to clear the string to begin with or declare a new one.
However, this doesn't work with non-letter characters, so here's my pseudocode for something that would do that:
const sarcasticText = str=> {
let newStr = '', numSkip = 0;
for(let i=0; i<str.length; i++) {
numSkip += str[i].match(/[a-z]/i) ? 0 : 1;
newStr += (i + numSkip) % 2 ? str[i].toUpperCase() : str[i].toLowerCase();
}
return newStr;
};
Basically, I'd implement a counter to check how many characters to offset.
Acc!!, 110 bytes
N*8
Count i while _/256 {
_+_/520*(727/_)*2+_/776*(983/_)*4
Write _/8+32*(_%4/3-_%8%5/4)
(_+(_%8+2)/4)%2+N*8
}
Algorithm
Set toLower flag to 0
Read a character
While not EOL:
Calculate isUpper and isLower flags
If isUpper && toLower, output (character + 32)
If isLower && !toLower, output (character - 32)
Else, output character unchanged
If isUpper or isLower, toggle toLower
Reset isUpper and isLower, and read a new character
Accumulator partitioning
We reserve the lowest three bits of the accumulator for storing the flags: toLower in the 1's place, isUpper in the 2's place, and isLower in the 4's place. The rest of the accumulator, at the 8's place and above, stores the input character.
For example, with input of Hello World, after reading the H and calculating flags, the accumulator has a value of 578, or in binary:
1001000 0 1 0
| | | \-> toLower (false)
| | \---> isUpper (true)
| \-----> isLower (false)
\----------> ASCII code for 'H'
After reading the space, the accumulator is 257:
0100000 0 0 1
| | | \-> toLower (true)
| | \---> isUpper (false)
| \-----> isLower (false)
\----------> ASCII code for ' '
Code
Line 1
N*8
Stores the first input character and sets all the flags to 0.
Line 2
Count i while _/256 {
Loops while the accumulator value is >= 256 (i.e. the character's ASCII code is at least 32).
Line 3
_+_/520*(727/_)*2+_/776*(983/_)*4
Calculates the isUpper and isLower flags. The main trick here is using integer division for the inequalities. For example, if x is a printable-ASCII code, then 65 <= x is equivalent to x/65 with integer division: values smaller than 65 become 0, values between 65 and 126 become 1, and we don't have to worry about anything larger than 126. We can use this approach for all four inequalities (keeping in mind that the ASCII codes are all multiplied by 8 in the accumulator).
isUpper:
65 <= char <= 90
65*8 <= acc <= 90*8+7
acc/520 == 1 && 727/acc == 1
Expression: _/520*(727/_)
isLower:
97 <= char <= 122
97*8 <= acc <= 122*8+7
acc/776 == 1 && 983/acc == 1
Expression: _/776*(983/_)
This line, then, takes the accumulator and adds isUpper*2 + isLower*4 to it.
Line 4
Write _/8+32*(_%4/3-_%8%5/4)
Outputs the character, swapping case if necessary.
There are two instances in which we need to modify the ASCII value before output:
- isUpper is 1, toLower is 1
- We need to convert the uppercase letter to lowercase by adding 32
- The three flag bits are 011, i.e. acc%4 == 3
- isLower is 1, toLower is 0
- We need to convert the lowercase letter to uppercase by subtracting 32
- The three flag bits are 100, i.e. acc%8 == 4
We can simulate the equality test using mod and int-div. For example, acc%8 == 4 is equivalent to acc%8%5/4:
acc%8 0 1 2 3 4 5 6 7
%5 0 1 2 3 4 0 1 2
/4 0 0 0 0 1 0 0 0
This line, therefore, takes 1 if we need to convert upper-to-lower, subtracts 1 if we need to convert lower-to-upper, multiplies that by 32, adds it to the ASCII code, and outputs it.
Line 5
(_+(_%8+2)/4)%2+N*8
Toggle the toLower flag if necessary; inputs and stores a new character.
We need to toggle toLower if the character was a letter, i.e. either isUpper or isLower was set. The possible flag bit values are 010, 011, 100, and 101--i.e. 2 through 5 (note that 6 and 7 are not possible because a letter can't be both upper- and lowercase). This expression gives 1 for those values and 0 otherwise:
_%8 0 1 2 3 4 5
+2 2 3 4 5 6 7
( )/4 0 0 1 1 1 1
Call this value isLetter. Then we want the new value of toLower to be toLower XOR isLetter (if isLetter is true, swap the value of toLower; otherwise, leave it unchanged). We can do XOR by adding the two values mod 2. Finally, we also read another character with N, multiply it by 8, and add it in.
C# (Visual C# Interactive Compiler), 84 bytes
int m,c;for(;(c=Read())>0;)Write((char)(c>64&c<91|c>96&c<123?m++%2>0?c|32:c&~32:c));
Modified version of The Lethal Coder's answer that takes advantage of some of the features of the interactive compiler.
J, 40 bytes
(2|+/\@:e.&Alpha_j_)`(tolower,:toupper)}
This follows almost directly from the definition of Composite Item (}):
We stack the upper and lowercase version of the input for our two possibilities using (tolower,:toupper).
Next we create a boolean list to indicate whether each character is alphabetic: e.&Alpha_j_ and take the scan sum of that: +/\@: which creates a monotonically increasing list which increases only on alphabetic characters. Finally we turn that into a boolean list where evens are 0 and odds are 1: 2|.
Putting those parts together means we alternate between the lower and uppercase versions of our input whenever we encounter a new alphabetic character, and only then.
Python 3, 95 bytes
Not nearly as golfed as @Rod's, but I'm posting it nevertheless.
lambda s:"".join([s[i],[s[i].lower(),s[i].upper()][i%2]][s[i].isalpha()]for i in range(len(s)))
Python 3, 192 bytes
x=list(input())
s=[]
for i in x[1::2]:
s.append(i)
x.remove(i)
s.reverse()
while len(x)<len(s):
x.append("")
while len(x)>len(s):
s.append("")
for i in range(len(x)):
print(end=x[i]+s[i])
Groovy, 79 bytes
{x=0;it.toUpperCase().collect{(it==~/\w/)?x++%2?it:it.toLowerCase():it}.join()}
Google Sheets, 264 bytes
=ArrayFormula(JOIN("",IF(REGEXMATCH(MID(A1,ROW(OFFSET(A1,0,0,LEN(A1))),1),"[A-Za-z]"),CHAR(CODE(UPPER(MID(A1,ROW(OFFSET(A1,0,0,LEN(A1))),1)))+MOD(LEN(REGEXREPLACE(LEFT(A1,ROW(OFFSET(A1,0,0,LEN(A1)))),"[^A-Za-z]","")),2)*32),MID(A1,ROW(OFFSET(A1,0,0,LEN(A1))),1))))
It's a big mess but it's a little easier if you expand it out:
=ArrayFormula(
JOIN(
"",
IF(REGEXMATCH(MID(A1,ROW(OFFSET(A1,0,0,LEN(A1))),1),"[A-Za-z]"),
CHAR(
CODE(UPPER(MID(A1,ROW(OFFSET(A1,0,0,LEN(A1))),1)))
+
MOD(LEN(REGEXREPLACE(LEFT(A1,ROW(OFFSET(A1,0,0,LEN(A1)))),"[^A-Za-z]","")),2)*32
),
MID(A1,ROW(OFFSET(A1,0,0,LEN(A1))),1)
)
)
)
The pseudo-logic would run like this:
For each character { // ArrayFormula()
If (character is a letter) { // REGEXMATCH(MID())
Return CHAR( // CHAR()
CODE(UPPER(letter)) // CODE(UPPER(MID()))
+
If (nth letter found and n is odd) {32} else {0} // MOD(LEN(REGEXREPLACE(LEFT())))
)
} else {
Return character // MID()
}
}
q/kdb+, 51 42 38 bytes
Solution:
{@[x;;upper]1#'2 cut(&)x in .Q.a}lower
Example:
q){@[x;;upper]1#'2 cut(&)x in .Q.a}lower"hello world"
"HeLlO wOrLd"
Notes:
.Q.a // abcde...xyz lowercase alphabet
(&) x in // where, returns indices for where x (hello world) is an alpha
2 cut // splits list into 2-item lists
1#' // takes first item of each 2-item list; ie the indices to uppercase
@[x;;upper] // apply (@) upper to x at these indices
C (tcc), 60 57 56 bytes
Thanks to DigitalTrauma for noticing bit 5 is the only difference for ASCII upper/lower case.
Special thanks to zch for golfing off three more bytes.
Save one more byte from RJHunter's idea
l;f(char*s){for(;*s=isalpha(*s)?*s&95|++l%2<<5:*s;s++);}
Haskell, 105 83 + 2 4 + 1 byte of separator = 108 86 88 Bytes
import Data.Char
f#(x:y)|isLetter x=([toUpper,toLower]!!f)x:(1-f)#y|1>0=x:f#y
_#l=l
Function is (1#), starts lowercase. Try it online!
The sad thing is that this is longer than the Java and C# answers Thanks to Ørjan Johansen for saving 22 bytes by merging three lines into one!
PowerShell, 86 bytes
-join($args[0]|%{if($_-match"[a-z]"-and($i=!$i)){"$_".toupper()}else{"$_".tolower()}})
Input is a [char[]] array.
Comments in code for explanation
# Join the array of string and char back together.
-join
# Take the first argument and pass each element ([char]) down the pipe.
($args[0]|%{
# Check if this is a letter. Second condition is a boolean that changes at every pass
# but only if the current element is a letter. If not the condition never fires
if($_-match"[a-z]"-and($i=!$i)){
# Change the character to uppercase
"$_".toupper()
}else{
# Output the character to lowercase.
# Special characters are not affected by this method
"$_".tolower()
}
})
Ruby, 57 55 47 41 bytes
Byte count includes two bytes for command line options.
Run it for example like this: $ ruby -p0 alternate_case.rb <<< "some input"
gsub(/\p{L}/){($&.ord&95|32*$.^=1).chr}
With the p0 option, the entire input is consumed in one go, and the magical global $. is incremented to 1. This is later toggled between 0 and 1 and used for keeping the state.
Works with multiline input; Try it online!
Thanks to Ventero for amazing input -- check the comments for details.
Alice, 18 bytes
/olZlYuN
@iy.u..//
Explanation
This program follows a lesser-known template for odd-length programs that run entirely in ordinal mode. The linearized version of this code is:
il.l.uN.YuZyo@
Explanation of code:
i - push input onto stack ["Hello world!"]
l - convert to lowercase ["hello world!"]
. - duplicate ["hello world!", "hello world!"]
l - convert to lowercase (should be no-op, but avoids what seems to be a bug in the TIO implementation)
. - duplicate again ["hello world!", "hello world!", "hello world!"]
u - convert to uppercase ["hello world!", "hello world!", "HELLO WORLD!"]
N - difference between sets ["hello world!", "helloworld"]
. - duplicate reduced string ["hello world!", "helloworld", "helloworld"]
Y - unzip (extract even positions) ["hello world!", "helloworld", "hlool", "elwrd"]
u - convert to uppercase ["hello world!", "helloworld", "hlool", "ELWRD"]
Z - zip evens back into string ["hello world!", "helloworld", "hElLoWoRlD"]
y - perform substitution ["hElLo WoRlD!"]
o - output []
@ - terminate
Without using l on the duplicate, the stack after N would be ["helloworld", "helloworld"]. I strongly suspect this is a bug.
GNU Sed, 33
- 5 bytes saved thanks to @TobySpeight
Score includes +1 for -r flag to sed.
s/([a-z])([^a-z]*.?)/\U\1\L\2/gi
C#, 100 bytes
s=>{var r="";int m=0;foreach(var c in s)r+=char.IsLetter(c)?(char)(++m%2>0?c|32:c&~32):c;return r;};
Python 3, 86 76 68 66 63 bytes
-2 bytes thanks to DJMcMayhem
-3 bytes thanks to Cyoce
x=0
for i in input():print(end=(2*i).title()[x]);x^=i.isalpha()
Java 8, 99 bytes
a->{String r="";int i=0;for(int c:a)r+=(char)(c>64&c<91|c>96&c<123?i++%2<1?c|32:c&~32:c);return r;}
Explanation:
a->{ // Lambda with char-array parameter and String return-type
String r=""; // Result-String
int i=0; // Flag for alteration
for(int c:a) // Loop over the characters of the input
r+=(char) // And append the result-String with the following (converted to char):
(c>64&c<91|c>96&c<123? // If it's a letter:
i++%2<1? // And the flag states it should be lowercase:
(c|32) // Convert it to lowercase
: // Else (should be uppercase):
(c&~32) // Convert it to uppercase
: // Else:
c); // Simply append the non-letter character as is
// End of loop (implicit / single-line body)
return r; // Return result-String
} // End of method
PHP 5, 54 bytes
<?=preg_filter('/\pL/e','($0|" ")^a^aA[$i^=1]',$argn);
Japt, 16 14 bytes
r"%l"_m"uv"gT°
Explanation
r // RegEx replace input
"%l" // [A-Za-z] as first arg to replace
_ // created function Z=>Z as second arg to replace
"uv"gT° // alternates "u" & "v"
m // map Z to either "u" upper or "v" lower
Perl 6, 32 30 bytes
{S:g/<:L><-:L>*<:L>?/$/.tclc()/}
{S:g{<:L><-:L>*<:L>?}=$/.tclc}
Expanded:
{ # bare block lambda with implicit parameter 「$_」
S # string replace (not in-place) implicitly against 「$_」
:global
{
<+ :L > # a letter
<- :L >* # any number of non-letters
<+ :L >? # an optional letter
}
=
$/.tclc() # uppercase the first letter, lowercase everything else
}
Jelly, 13 bytes
nŒsTm2
ŒlŒuǦ
How it works
ŒlŒsǦ Main link. Argument: s (string)
Œl Cast to lowercase.
Ǧ At indices returned by the helper link...
Œu apply uppercase.
nŒsTm2 Helper link. Argument: s (string)
Œs Apply swapcase to s.
n Perform vectorizing not-equal comparison.
T Compute the truthy indices.
m2 Select every other one, starting with the first.
Pyth, 11 bytes
srR~xZ}dGrZ
Explanation
# Z = 0; Q = eval(input())
srR~xZ}dGrZQ # Auto-fill variables
rZQ # lowercase the input
rR # Apply the r function to each letter of the input with
~xZ}dG # ... this as the other argument
~ # use the old value of the variable Z, then update it with the value of ...
xZ # Z xor ...
}dG # the variable d is a lowercase letter
# because of how mapping works in pyth, d will contain the current letter
# This causes Z to flip between 0 and 1, alternately upper and lower casing
# the current character if it is a letter
Retina, 32 bytes
T`l`L
01T`L`l`[A-Z][^A-Z]*[A-Z]?
First converts the input to uppercase, and then groups the input into matches containing up to two capital letters. The only time it will contain only one letter is if the last letter doesn't have a pair. Then it lowercases the first letter of each of these matches.
The 01 in the second stage translates roughly to: do not change the behaviour of this stage based on the match number, but only apply the changes to the first character of each match.
C 64 bytes
B;R(char *s){for(;*s=isalpha(*s)?(B=!B)?*s|=32:*s&=~32:*s;s++);}
Takes advantage of ascii encoding where upper and lower case letters are offset by 0x20.
CJam, 26 24 bytes
qeu{_'[,65>&,T^:T{el}&}%
Explanation
q e# Read all input.
eu e# Uppercase it.
{ e# For each character:
_ e# Duplicate it.
'[,65>& e# Set intersection with the uppercase alphabet.
, e# Length (either 0 or 1 in this case).
T^:T e# XOR with T (T is initially 0), then store the result back in T.
{el}& e# If The result of the XOR is true, lowercase the character.
}% e# (end for)
MATL, 16 15 bytes
Xktkyy-f2L))5M(
Try it online! Or verify all test cases.
Explanation
Consider input 'hello world'
Xk % To upper case
% STACK: 'HELLO WORLD'
t % Duplicate top element
% STACK: 'HELLO WORLD', 'HELLO WORLD'
k % To lower case
% STACK: 'HELLO WORLD', 'hello word'
yy % Duplicate top two elements
% STACK: 'HELLO WORLD', 'hello word', 'HELLO WORLD', 'hello word'
- % Difference (of code points; element-wise)
% STACK: 'HELLO WORLD', 'hello word', [-32 -32 -32 -32 -32 0 -32 -32 -32 -32 -32]
f % Indices of nonzeros
% STACK: 'HELLO WORLD', 'hello word', [1 2 3 4 5 7 8 9 10 11]
2L) % Keep only even-indexed values (*)
% STACK: 'HELLO WORLD', 'hello word', [2 4 7 9 11]
) % Reference indexing (get values at indices)
% STACK: 'HELLO WORLD', 'elwrd'
5M % Push (*) again
% STACK: 'HELLO WORLD', 'elwrd', [2 4 7 9 11]
( % Assignment indexing (write values at indices). Implicit display
% STACK: 'HeLlO wOrLd
'
05AB1E, 11 8 bytes
Code:
lvyJ¤aiš
Uses the 05AB1E encoding. Try it online!
Explanation:
l # Lowercase the input
vy # For each element..
J # Join the entire stack into a single string
¤a # Check if the last character is alphabetic
iš # If true, swapcase the entire string
PHP, 71 Bytes
for(;a&$c=$argn[$i++];)echo ctype_alpha($c)?(ul[$k++&1].cfirst)($c):$c;
Brachylog, 25 bytes
{ḷ|ụ}ᵐ.{ḷ∈Ạ&}ˢ¬{s₂{∈Ạ}ᵐ}∧
This is both long and slow.
Explanation
{ }ᵐ. The Output is the result of mapping on each char of the Input:
ḷ Lowecase the char
| Or
ụ Uppercase the char
{ }ˢ In the Ouput, select the chars that:
ḷ∈Ạ& when lowercased are in "abc...xyz" (ie are letters)
¬{ }∧ In that new string, it is impossible to find:
s₂ a substring of 2 consecutive chars
{∈Ạ}ᵐ where both of them are in the lowercase alphabet
V, 17, 13 bytes
VUÍá¨áü$©/ì&
HeXdUmP:
00000000: 5655 cde1 83a8 e1fc 24a9 2fec 26 VU......$./.&
Explanation:
This uses a compressed regex™️, so before explaining it, let's expand the regex out:
:%s/\v\a.{-}(\a|$)/\l&
The VU converts everything to uppercase. Then we run this:
:% " On every line:
s/\v " Substitute:
\a " A letter
.{-} " Followed by as few characters as possible
(\a|$) " Followed by either another letter or an EOL
/ " With:
\l " The next character is lowercased
& " The whole text we matched
Old/more interesting answer:
:se nows
Vuò~h2/á