| Bytes | Lang | Time | Link |
|---|---|---|---|
| 055 | AWK | 250807T163148Z | xrs |
| 017 | Uiua | 231105T212309Z | chunes |
| 035 | APOL | 220106T173705Z | Ginger |
| nan | Javascript | 220102T092832Z | atzlt |
| 065 | Factor | 220207T034305Z | chunes |
| 142 | Headascii | 220206T214432Z | thejonym |
| 011 | 05AB1E | 220105T183623Z | Kevin Cr |
| 041 | Ruby | 220104T110724Z | daniero |
| 086 | Python 2 | 220102T071538Z | U13-Forw |
| 123 | SNOBOL4 CSNOBOL4 | 220102T191426Z | Giuseppe |
| 022 | x86 / x8664 machine code | 220103T184510Z | Peter Co |
| 3331 | J | 220102T233605Z | Jonah |
| 014 | Jelly | 220102T155948Z | lynn |
| 019 | BQN | 220102T110451Z | Razetime |
| 058 | Python | 220102T072327Z | pxeger |
| 061 | C gcc | 220102T103833Z | dingledo |
| 057 | R | 220102T194622Z | Dominic |
| 094 | R | 220102T125535Z | Maë |
| 065 | R | 220102T191837Z | pajonk |
| 010 | Japt v2.0a0 | 220102T182618Z | Shaggy |
| 111 | C tcc | 220102T163629Z | Finxx |
| 081 | Lua | 220102T162032Z | Luatic |
| 025 | Retina 0.8.2 | 220102T121206Z | Neil |
| 039 | APL+WIN | 220102T092932Z | Graham |
| 021 | Charcoal | 220102T083417Z | Neil |
| 023 | Dyalog APL | 220102T083533Z | Adá |
| 017 | Retina | 220102T081906Z | Neil |
| 009 | Vyxal | 220102T075406Z | lyxal |
| 023 | Perl 5 | 220102T072718Z | Dom Hast |
| 013 | Jelly | 220102T072339Z | hyperneu |
Uiua, 17 bytes
-×32×\≠.∊,"aeiou"
Port of Razetime's BQN answer.
-×32×\≠.∊,"aeiou"
∊,"aeiou" # mask of where vowels are in input
. # duplicate
\≠ # inequality scan
× # multiply
×32 # multiply by 32
- # subtract (capitalize)
APOL, 35 bytes
j(ƒ(i ¿(&(c(Ⓔ ↓(∋)) ≐(∈)) ∋ ↑(∋))))
Explanation
j( Join a list to a string
ƒ( Listbuilder for
i Input
¿( Returning if (called for each item in the input)
&( And (condition)
c( String contains
Ⓔ The constant string "aeiou"
↓( To lowercase
∋ Current character
)
)
≐( Is even
∈ For loop counter
)
)
∋ For loop item (executed if true)
↑( To uppercase
∋ For loop item
)
)
)
)
Javascript, 57 5654 bytes
s=>s.replace(/[aeiou]/g,c=>(s=!s)?c.toUpperCase():c)
-1 Thanks to Patrick Stephansen.
-2 Thanks to tsh.
-2 Thanks to l4m2.
Usage:
console.log((s=>s.replace(/[aeiou]/g,c=>(s=!s)?c.toUpperCase():c)))("string goes here"))
Factor, 65 bytes
[ dup [ "aeiou"in? ] arg-where <odds> over [ 32 - ] change-nths ]
Explanation
Get the indices of the vowels from the input, take the odd-indexed elements of those, then subtract 32 from the input at those indices. change-nths is a mutating word which has the usual ( indices seq -- ) stack effect which is why we need an extra copy on the stack before we call it.
! "abcdefghijklmnopqrstuvwxyz"
dup ! "abcdefghijklmnopqrstuvwxyz" "abcdefghijklmnopqrstuvwxyz"
[ "aeiou"in? ] arg-where ! "abcdefghijklmnopqrstuvwxyz" V{ 0 4 8 14 20 }
<odds> ! "abcdefghijklmnopqrstuvwxyz" { 4 14 }
over ! "abcdefghijklmnopqrstuvwxyz" { 4 14 } "abcdefghijklmnopqrstuvwxyz"
[ 32 - ] change-nths ! "abcdEfghijklmnOpqrstuvwxyz"
Headascii, 142 bytes
----[]]]][]][++++^^^^D^^^^^+^{{D^(U):++++(R):++++(R):++++++(R):++++++(R):R()+E:P};RP{D^(U):++++(R):++++(R):++++++(R):++++++(R):R()+E:P};R]P}.!
Try it here! Code will have to be copied, and executed like this:
erun("----[]]]][]][++++^^^^D^^^^^+^{{D^(U):++++(R):++++(R):++++++(R):++++++(R):R()+E:P};RP{D^(U):++++(R):++++(R):++++++(R):++++++(R):R()+E:P};R]P}.!","your input")
This one was fun, not sure if there's any room for serious golfing without a different approach. Maybe something with an oscillator to decide whether to uppercase a given vowel? But the 3 main storage registers are used up for comparison, storing -32 (uppercase constant), and storing 97 (ascii a, first vowel). So I'm not sure where the constant would be held. I'll take another whack at it sometime though.
----[]]]][]][++++^^^^D^^^^^+^ Constants -32 and 97
---- -4
[]]]] *4
[]][ *2, and store
++++ 4
^^^^ *4
D^^^^^+^ *6+1, already stored
{{...};RP{...};R]P}. The rest of block 0
{ } Loop
{ } Loop until a vowel is found
;RP Concatenate it to the string register
{ } Loop until a vowel is found
;R]P Subtract 32 (uppercase) and concatenate to string register
. Block separator
{D^(U):++++(R):++++(R):++++++(R):++++++(R):R()+E:P} First loop
{ } Loop
D^(U) If byte is "a",
++++(R) "e"
++++(R) "i"
++++++(R) "o"
++++++(R) or "u",
: : : : : Exit loop.
Else,
R() If byte is null (i.e. string ended)
+E Go to block 1
: Else,
P Concatenate byte to string register
{D^(U):++++(R):++++(R):++++++(R):++++++(R):R()+E:P} Second Loop is identical to the first
Avoiding two identical loops would be
a major golfing opportunity
! Block 1
! Print string register, end of execution
05AB1E, 11 bytes
žMÃDεNÉiu]‡
Or alternatively the εNÉiu] could be 2ι`u.ι.
Explanation:
žM # Push the constant "aeiou"
à # Only keep those letters from the (implicit) input
D # Duplicate it
ε # Map the vowels in the copy to:
NÉi # If the 0-based index is odd:
u # Uppercase the vowel
] # Close the if-statement and map
‡ # Transliterate the lowercase vowels to the alternating cased vowels
# in the (implicit) input
# (after which the result is output implicitly)
2ι # Uninterleave into two parts
` # Pop and push both parts separated to the stack
u # Uppercase the second part
.ι # And interleave the two parts back again (to a list of characters)
Python 2, 86 bytes
-26 thanks to ElPedro.
-2 thanks to Daniel.
c,S=0,''
for i in input():
if i in'aeiou':S+=(i,i.upper())[c];c^=1
else:S+=i
print S
SNOBOL4 (CSNOBOL4), 148 123 bytes
S =INPUT
N S ARB . L ANY('aeiou') . C REM . S :F(O)
X =1 - X
C =CHAR(ORD(C) - 32) EQ(X)
O =O L C :(N)
O OUTPUT =O S
END
Explanation:
S =INPUT ;* Read input
N S ARB . L ANY('aeiou') . C REM . S :F(O)
;* Match in S:
;* ARBitrary string (store as L), ANY single vowel (store as C)
;* and the REMainder of the string (store as S).
;* If there is no match, goto O.
X =1 - X ;* Set X (initially treated as 0) to 1-X.
C =CHAR(ORD(C) - 32) EQ(X) ;* if X == 0, set C to uppercase, otherwise do nothing.
O =O L C :(N) ;* append to O L and C, then goto N.
O OUTPUT =O S ;* Output updated string
END
x86 / x86-64 machine code, 22 bytes
Takes pointer (RSI), length (RCX) in registers, modifies the string in-place. Callable from C with the x86-64 SysV calling convention as void vowel_alt_caseflip(int dummy, char *RSI, int dummy, size_t RCX). Same machine code works in 32-bit mode, although none of the standard 32-bit C calling conventions match.
NASM listing, with address, machine-code bytes, and source the machine-code answer was generated from.
1 ;;; char *str /* RSI */, size_t len /* RCX */
2 vowel_alt_caseflip:
3 00000000 BF22822000 mov edi, 0x0208222 ; ASCII vowel bitmap, 1-indexed like ASCII codes are with 'a' = 0x61 not 0x60
4 00000005 31D2 xor edx, edx ; first vowel is not flipped
5 .loop:
6 00000007 AC lodsb ; al = *str++
7 00000008 0FA3C7 bt edi, eax ; if (bitmap & (1<<(al&31)))
8 0000000B 7306 jnc .non_vowel
9 0000000D 3056FF xor [rsi-1], dl ; caseflip or not the current character
10 00000010 80F220 xor dl, 0x20 ; toggle state for the next
11 .non_vowel:
12 00000013 E2F2 loop .loop
13 00000015 C3 ret
; next address is 0x16, size is 0x16 = 22 bytes
Since the input string is guaranteed to be all lowercase alphabetic, I just used XOR instead of AND with ~0 / ~0x20 to flip instead of clear the ASCII lower-case bit. It would work out to the same size with mov dl, 0xFF / and [rsi-1], dl.
Try it online! with a Linux _start caller that passes it the alphabet twice and makes write system calls before/after, so we can see that it correctly treats u as a vowel.
For more about the immediate bitmap strategy, and variations like using it branchlessly, see SO and two of my previous x86 answers:
- GCC turning a
switch()statement intobton a register, in an SO answer - User Appreciation Challenge #1: Dennis ♦
- Vowels up, consonants down
Alternate version, same size
I had hoped to be able to use xor al, 0x20 to save a byte vs. DL, but we need AL for efficient string-reading via lodsb. (We need a char from the string in a register to use as a source for bt.) We could start with mov there and use stosb to store, but that would have to be separate from a reg-reg XOR or AND, and still doesn't free up AL.
We could use scasb as a 1-byte inc rdi. Using mov dl, [rdi] to start, and saving the -1 in the memory-dest addressing mode, and another byte from xor al, 0x20 short-form, that's break-even for size and worse for efficiency (useless scasb reload). Or similar efficiency in 32-bit mode since we could use inc edi there.
;;; char *str /* RDI */, size_t len /* RCX */
vowel_alt_caseflip_scasb:
mov esi, 0x0208222 ; ASCII vowel bitmap, 1-indexed like ASCII codes are with 'a' = 0x61 not 0x60
xor eax, eax ; first vowel is not flipped
.loop:
mov dl, [rdi]
bt esi, edx ; if (bitmap & (1<<(c&31)))
jnc .non_vowel
xor [rdi], al ; caseflip or not the current character
xor al, 0x20 ; toggle state for the next
.non_vowel:
scasb ; inc rdi in one byte even on x86-64
loop .loop
ret
J, 33 31 bytes
(>~:/\)@e.&'aeiou'`(,:toupper)}
-2 thanks to Lynn
Kind of shockingly long, but best I could find so far...
Jelly, 14 bytes
e€Øcn\<$a32^OỌ
e€Øc For each input character: is it in Øc (vowels)?
This gets a mask with 1s where vowels are, e.g.
“b x d e f h i z k o m u p”
0 0 0 1 0 0 1 0 0 1 0 1 0
n\<$ scanl(≠, mask) < mask: This selects every other 1.
scanl(≠): 0 0 0 1 1 1 0 0 0 1 1 0 0
original: 0 0 0 1 0 0 1 0 0 1 0 1 0
<: 0 0 0 0 0 0 1 0 0 0 0 1 0
a32 Replace all 1s by 32.
^O XOR by ord(input string).
Ọ chr()
BQN, 19 bytesSBCS
⊢-32×·≠`⊸<∊⟜"aeiou"
Textbook use for under, but character arithmetic is much, much shorter. -10 from Marshall, then -3 from Marshall.
Explanation
⊢-32×·≠`⊸<∊⟜"aeiou"
∊⟜"aeiou" bitmask of vowels
≠` inequality scan(XOR scan)
⊸< lesser than original bitmask?
32×· 32 × that
⊢- subtract from the input
Python, 73 71 ··· 58 bytes
lambda s,c=0:bytes(a^c|(c:=c^34087456>>a-97&32)for a in s)
Inputs and outputs as a byte string.
Explanation (I use a $ sigil on variables to distinguish them from letters):
- Consider the lookup table for "letter
$ais a vowel": this is the binary array10001000100000100000100000(the1s are at the alphabet positions ofaeiou) - If we store this as an integer (converting the binary array into a decimal number
35784736), we can lookup into it using35784736 >> 97 - alphabet_index($a) & 1 - If
$ais actually the ASCII code of the character, we can get its alphabet index using$a - 97(since'a' == 97) - By taking input and output as
bytesin Python, we can get this ASCII code basically for free - We can reverse the lookup table to make it shorter by changing
$a - 97to97 - $a, because by reversing it the trailing zeroes (forvwxyz) become leading zeroes and make the number smaller - The reverse lookup table is
00000100000100000100010001 = 100000100000100010001, or1065233in decimal, giving us1065233 >> $a - 97 & 1 - We can toggle the flag variable
$cif$ais a vowel, using$c := $c ^ (1065233 >> $a - 97 & 1) - We also want to uppercase
$asometimes. Observe that the uppercase ASCII letters differ from the lowercase letters by exactly32, so we can toggle the case of$ausing$a ^ 32 - The case toggling should happen if both:
$cis on (since we initialise it to0, and every vowel starting with the second should be capitalised)$ais a vowel
- This is equivalent to
$a ^ 32 * ($c & (1065233 >> $a - 97 & 1)) - If we store "on" as
32rather than just1, and we adjust the lookup table to return32/0rather than1/0, we'll save bytes. - We can do this by using
1065233 * 32 = 34087456as the magic number and&-ing with32instead of1. The resulting expression is34087456 >> ($a - 97) & 32 - We want to avoid using
34087456 >> ($a - 97) & 32in more than one place, because it's a long expression - Since we're modifying
$cwith a:=expression, we can do a computation on its value before and after modification to extract the correct value. Let$cbe the value before modification and$c'be the new value $c'is different to$cif and only if$ais a vowel, so we want to capitalise if$cis on and$c' != $c, i.e.$cis on and$c'is off- Recall that lowercase letters have the
32s bit set, and to uppercase a letter we need to unset it. This means$ahas the32s bit set initially - By
xoring$aand$c, when$cis on, we remove the32s bit, uppercasing the letter - But we only want to do this when
$ais a vowel, so we can re-set the32s bit when it isn't. Thus by adding| $c', the32s bit is added again when$c'is32. This is the case if$cwas32and$a'was not a vowel, so the bit is only removed when$ais a vowel and$cis32
C (gcc), 61 bytes
A full program, where input is taken from STDIN.
x;main(c){for(;read(0,&c,1);)putchar('Xz'%c?c:c^x++%2*32);}
Checking for vowels
Vowels are aeiou, which have the corresponding ASCII codes 97,101,105,111,117. The LCM of these numbers is 1484392455, which has the property of being evenly divisible by only the letters aeiou. Therefore, we can say that c is a vowel if 1484392455%c equals 0. We can compress this number by the use of multi-character constants, giving 'Xz\x08\x07'.
C (clang), 48 bytes
As offered by @AZTECCO, a function which takes a wide string as input, and modifies the string in-place.
x;f(*s){for(x=0;*s;)*s++^='Xz'%*s?0:x++%2*32;}
R, 57 bytes
function(s,t=s%in%utf8ToInt("aeiou"))s-t*32*!cumsum(t)%%2
Input is vector of character codes (for comparison to pajonk's answer). Would be 77 bytes with input as string (for comparison to Maël's answer).
Edit: this approach would be only 48 bytes by porting dingledooper's great modulo trick...
R, 94 bytes
-26 bytes thanks to Giuseppe. -2 bytes thanks to Robin Ryder
function(s){s=el(strsplit(s,""))
s[i]=toupper(s[i<-grep("[aeiou]",s)[!1:0]])
Reduce(paste0,s)}
See pajonk's and Dominic van Essen's answers for a clever use of intToUtf8 and utf8ToInt.
R, 65 bytes
function(s){s[i]=s[i<-which(s%in%utf8ToInt("aeiou"))[!1:0]]-32;s}
Takes input and outputs as vectors of char codes.
Some parts borrowed from @Maël's R answer.
C (tcc), 174 111 bytes
Saved 63 bytes thanks to manatwork
i,b;f(char*a,int l){for(i=0;i<l;i++)if(a[i]==97||a[i]=='e'||a[i]=='i'||a[i]=='o'||a[i]=='u')if(b++%2)a[i]-=32;}
Unminified
i, b;
f(char* a, int l) {
for (i = 0; i < l; i++)
if (a[i] == 97 || a[i] == 'e' || a[i] == 'i' || a[i] == 'o' || a[i] == 'u')
if (b++ % 2)
a[i] -= 32;
}
Lua, 81 bytes
print(((...):gsub("[aeiou]",function(x)u=not u;return u and x or(x):upper()end)))
Expects the input as first argument, prints output. Is one character shorter if return instead of print is used.
Retina 0.8.2, 25 bytes
0T-1=`l`L`(.*?[aeiou]){2}
Try it online! Link includes test cases. Explanation:
(.*?[aeiou]){2}
Match the shortest possible substrings containing two vowels.
-1=
Only transliterate the last character of each match. (The 0 indicates that this modifier applies to the characters of the match rather than the matches themselves.)
T`l`L`
Upper case them.
APL+WIN, 39 bytes
Prompts for string
⎕av[(⎕av⍳s)-32×1=i+i×≠\i←(s←⎕)∊'aeiou']
Index offset from lower to upper case changed in TIO from -32 to +48 to account for differences between APL+WIN and Dyalog atomic vectors.
Charcoal, 21 bytes
⭆S⎇№aeiouι§⁺↥ιιL⊞Oυιι
Try it online! Link is to verbose version of code. Explanation:
S Input string
⭆ Map over characters and join
№ Count of
ι Current character
aeiou In literal string `aeiou`
⎇ If exists then
ι Current character
↥ Uppercase
⁺ Plus
ι Current character
§ Indexed by
ι Current character
⊞O Pushed to
υ Predefined empty list
L Length after push
ι Otherwise current character
Implicitly print
Dyalog APL, 23 bytes
1∘⎕C@(=\≠⍨)@(∊∘'aeiou')
1∘⎕C uppercase…
@(…) at…
=\ XNOR scan (i.e. every other) of the
≠⍨ XOR selfie (i.e. all 0s)
@(…) at…
∊ member
∘ of
'aeiou' vowels
Retina, 17 bytes
1,2,T`l`L`[aeiou]
Try it online! Link includes test cases. Explanation:
[aeiou]
Match lower case vowels.
1,2,
Only process every other vowel.
T`l`L`
Upper case them.
Although the transliterate command has a shortcut code for vowels, they aren't of any help here so I haven't used them.
Vyxal, 9 bytes
ATy$_⁽⇧¨M
The power of triads!
Explained
ATy$_⁽⇧¨M
AT # indices of vowels
y # uninterleave into two lists - this pushes a list of every second vowel and every first vowel
$_ # remove the list of every first vowel
⁽⇧¨M # and apply upper-case (a function pushed by ⁽⇧) to those indices (¨M)
Perl 5, 23 bytes
s/[aeiou]/$&^$"x$|--/ge
Explanation
s///ubstitutes each of the lowercase vowels with itself XORed with either 1 or 0 spaces ($" defaults to space) based on $|-- which will alternate each time it's decremented.
Jelly, 13 bytes
e€ØẹTḊm2
Œuç¦
e€ØẹTḊm2 Helper Link; get every other vowel
e€ For each element, is it in
Øẹ the list of vowels ("aeiou")?
T Get said indices
Ḋm2 Pop off the first one and take every other one
Œuç¦ Main Link
¦ Apply
Œu to-uppercase
ç to every other vowel