g | x | w | all
Bytes Lang Time Link
059Zsh241107T050839Zroblogic
008APL Dyalog Extended241107T154520ZAdá
026QuadR241107T142219ZAdá
063AWK241106T205035Zxrs
128Casio BASIC casio fx9750GIII241019T183524Zmadeforl
062JavaScript ES6170525T172037ZArnauld
008Uiua SBCS240725T130745Zchunes
055R240725T115837Zint 21h
103brev220818T124013ZSandra
008Vyxal r220721T031907ZnaffetS
029SM83220721T020523ZNoLonger
098MUMPS220720T041856ZRyan Pat
017Perl 5 p220718T060801ZSisyphus
010Japt v2.0a0191222T231007ZShaggy
104Javascript200611T230832ZJustin
110Acc!!191223T083125ZDLosc
084C# Visual C# Interactive Compiler190227T090554Zdana
040J190227T050840ZJonah
024Perl 5170706T074148ZDom Hast
095Python 3170706T081011ZFedone
192Python 3170528T005552Zuser6927
079Groovy170613T181926ZMagic Oc
264Google Sheets170613T175340ZEngineer
038q/kdb+170527T131015Zmkst
056C tcc170525T174649Zcleblanc
088Haskell170529T021058ZGeneric
086PowerShell170527T210211ZMatt
041Ruby170525T204251Zdaniero
018Alice170527T045603ZNitrodon
033GNU Sed170525T173530ZDigital
100C#170526T111855ZTheLetha
063Python 3170525T172834ZRod
099Java 8170526T074153ZKevin Cr
054PHP 5170526T024755Zuser6395
016Convex170525T231611ZGamrCorp
014Japt170525T175456Zpowelles
nan170525T214937ZBrad Gil
013Jelly170525T172139ZDennis
011Pyth170525T200552ZFryAmThe
032Retina170525T194819ZFryAmThe
064C170525T183821Zuser2301
024CJam170525T180514ZBusiness
015MATL170525T172714ZLuis Men
00805AB1E170525T174539ZAdnan
071PHP170525T181554ZJör
025Brachylog170525T180147ZFatalize
013V170525T172423ZDJMcMayh
046Retina170525T174010ZNeil

Zsh, 64 59 bytes

for x (${(Us::)1}){${x/[A-Z]}&&((++n%2))&&x=$x:l;printf $x}

Try it online!  64 bytes

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

⊢×≠\⍤|⍤×

Try it online!

 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

Try it online!

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.

Try it online!

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))

Try it online!

Uiua SBCS, 8 bytes

⍜▽¯\≠⊸±⌵

Try it!

⍜▽¯\≠⊸±⌵­⁡​‎‎⁡⁠⁣⁡‏‏​⁡⁠⁡‌⁢​‎‎⁡⁠⁢⁣‏⁠‎⁡⁠⁢⁤‏‏​⁡⁠⁡‌⁣​‎‎⁡⁠⁢⁡‏⁠‎⁡⁠⁢⁢‏‏​⁡⁠⁡‌⁤​‎‎⁡⁠⁤‏‏​⁡⁠⁡‌⁢⁡​‎‎⁡⁠⁡‏⁠‎⁡⁠⁢‏⁠‎⁡⁠⁣‏‏​⁡⁠⁡‌­
       ⌵  # ‎⁡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)

Attempt This Online!

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?

Vyxal r, 8 bytes

ʀTy?⇩⁽⇧V

Try it Online!

The r flag shouldn't be needed, but bugs, bugs, bugs.

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

Try it online!

Both lines have a leading space so that their first characters are interpreted as commands and not labels.

Perl 5 -p, 17 bytes

s/\pL/<`>|uc$&/ge

Try it online!

Japt v2.0a0, 11 10 bytes

r\lÈgYguiv

Try it

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
}

Try it online!

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:

  1. 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
  2. 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));

Try it online!

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)}

Try it online!

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.

Perl 5, 24 bytes

23 bytes + 1 byte for -p.

Thanks to @Dada for -2 bytes.

s/\pl/--$|?uc$&:lc$&/eg

Try it online!

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)))

Try it online!

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])

Try it online!

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++);}

Try it online!

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..//

Try it online!

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

Score includes +1 for -r flag to sed.

s/([a-z])([^a-z]*.?)/\U\1\L\2/gi

Try it online.

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()

Try it online! or Try all test cases

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:

Try it here.

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);

Convex, 16 bytes

¯{_U&,R^:R{¬}&}%

Try it online!

Convex port of @Business Cat's answer.

Japt, 16 14 bytes

r"%l"_m"uv"gT°

Try it online!

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()/}

Try it

{S:g{<:L><-:L>*<:L>?}=$/.tclc}

Try it

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Ǧ

Try it online!

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

Try it here

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]?

Try it online!

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}&}%

Try it online!

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;

Try it online!

Brachylog, 25 bytes

{ḷ|ụ}ᵐ.{ḷ∈Ạ&}ˢ¬{s₂{∈Ạ}ᵐ}∧

Try it online!

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Íშáü$©/ì&

Try it online!

Or Verify all test cases!

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/á

Retina, 46 bytes

T`L`l
T`l`L`.(?=([^a-z]*|[a-z][^a-z]*[a-z])*$)

Try it online! Includes test cases.