| Bytes | Lang | Time | Link |
|---|---|---|---|
| 057 | AWK | 241216T164047Z | xrs |
| 019 | Pyth | 241214T213700Z | ErikDaPa |
| 012 | Kakoune | 241214T030104Z | JoshM |
| 013 | Uiua | 241213T190916Z | Joao-3 |
| 381 | jBasher2 | 240924T165918Z | madeforl |
| 013 | UiuaSBCS | 240927T135117Z | Europe20 |
| 014 | Uiua | 240925T122747Z | nyxbird |
| 045 | PHP | 240622T115334Z | Glory2Uk |
| 140 | YASEPL | 240212T194232Z | madeforl |
| 6359 | JS 63 .. 59 Bytes | 240304T085512Z | Adam Bas |
| 9459 | Scala 3 | 231225T091523Z | 138 Aspe |
| 009 | Pip | 240302T012643Z | DLosc |
| 088 | Swift | 231231T195053Z | macOSist |
| 014 | Uiua SBCS | 240212T060119Z | chunes |
| 045 | R | 231231T200142Z | Giuseppe |
| 045 | APLNARS | 231225T184731Z | Rosario |
| 125 | Swift | 231225T161414Z | Annett S |
| 036 | Gema | 231225T033148Z | manatwor |
| 4375 | Vyxal s | 230506T125201Z | lyxal |
| 005 | Vyxal 3 s | 231225T002434Z | pacman25 |
| 064 | Lua | 230507T134157Z | bluswimm |
| 008 | Thunno 2 J | 230506T131651Z | The Thon |
| 043 | Arturo | 230505T185756Z | chunes |
| 039 | Zsh extendedglob | 221107T172101Z | roblogic |
| 068 | Python 3 | 220305T093319Z | Sapherey |
| 007 | Vyxal | 220222T192154Z | Seggan |
| 075 | Excel VBA | 220216T165756Z | Engineer |
| 009 | Brachylog | 220216T112455Z | Fatalize |
| 019 | Vim | 220216T185629Z | DLosc |
| 111 | Lexurgy | 220216T155321Z | bigyihsu |
| 017 | APL Dyalog Unicode | 220216T130900Z | RGS |
| 020 | Pyth | 220216T124715Z | Wallbrea |
| 057 | Funky2 | 220216T102129Z | ATaco |
| 065 | SML | 200523T231537Z | Vibius |
| 082 | Python 3 | 200523T231153Z | Adam Aba |
| 049 | Julia | 200523T221245Z | jling |
| 021 | K oK | 200509T192824Z | mkst |
| 024 | K4 | 200509T193634Z | mkst |
| 050 | Python 3 | 200401T011848Z | Bubbler |
| 070 | Python 3 | 200324T054122Z | yetanoth |
| 9377 | JavaScript V8 | 200325T084247Z | sportzpi |
| 068 | Haskell | 200325T150123Z | david |
| 053 | Python | 200325T003534Z | xnor |
| 084 | Python 3 | 200324T143100Z | Dion |
| 097 | Javascript V8 | 200324T125820Z | Aesahaet |
| 055 | Python 3 | 200322T160611Z | Surculos |
| 048 | TSQL | 200323T194931Z | BradC |
| 019 | Perl 5 p | 200322T164406Z | Xcali |
| 023 | Bash + Core utilities | 200322T205733Z | Mitchell |
| 033 | Ruby | 200322T172351Z | G B |
| 006 | 05AB1E | 200322T163617Z | Grimmy |
| 049 | C gcc | 200322T194007Z | Arnauld |
| 115 | Batch | 200323T104133Z | T3RR0R |
| 021 | x8664 machine code | 200323T080001Z | Peter Co |
| 034 | Java 8 | 200323T081930Z | Kevin Cr |
| 045 | JavaScript Node.js | 200322T173150Z | Arnauld |
| 046 | x86 platformindependent machine code | 200322T175258Z | Kamila S |
| 082 | Red | 200322T192341Z | Galen Iv |
| 009 | W d | 200323T022148Z | user9206 |
| 072 | Io | 200323T011933Z | user9206 |
| 024 | sed | 200322T175950Z | Noodle9 |
| 058 | C gcc | 200322T163744Z | S.S. Ann |
| 071 | C gcc | 200322T163417Z | Noodle9 |
| 014 | Charcoal | 200322T175802Z | Neil |
| 009 | Retina | 200322T175405Z | Neil |
| 014 | APL Dyalog Extended | 200322T174702Z | Adá |
| 015 | CJam | 200322T171644Z | Luis Men |
| 006 | 05AB1E | 200322T170810Z | Jonathan |
| 007 | Japt v2.0a0 | 200322T164206Z | Shaggy |
| 007 | Jelly | 200322T163218Z | Jonathan |
AWK, 96 57 bytes
{for(;i++<NF;)printf$i~/[aeiou]/?toupper($i):tolower($i)}
{a["a"]="A";a["e"]="E";a["i"]="I";a["o"]="O";a["u"]="U"
$0=tolower($0);for(i in a)gsub(i,a[i])}1
Pyth, 19 bytes
sm?}d"aeiou"rd1drz0
sm?}d"aeiou"rd1drz0
rz0 => .lower()
?}d"aeiou" => if it's a vowel
rd1 => capitalize it
d => else keep it as it is
m => do it to every letter
s => list to str
Kakoune, 24 17 12 bytes
%`s[aeiou]<ret>~
%: select entire buffer`: lowercase everythings[aeiou]<ret>: create selection for every vowel~: uppercase vowels
OLD:
%s[aeiouAEIOU]<ret>~%s<c-p><a-b><a-b>^<ret>`
ignore trailing space (how do I escape a backtick at the end of a markdown code block?)
%: select entire buffers[aeiouAEIOU]<ret>: create a selection for every vowel matched~: uppercase selections%: select entire buffers: create selections for each regex match<c-p>: go to previous entry<a-b><a-b>^<ret>navigate to just after[in regex and add^to negate it`: lowercase selections
Uiua, 13 bytes(SBCS)
⍥¯¬⊸∊"AEIOU"⌵
Apparently, every other Uiua solution overlooks the fact that ⍥ repeat can be used pervasively, in other words, it implicitly does ≡ rows.
jBasher2, 437 381 bytes
turns out there were a few things that weren't needed.
create a with type string
ask for input
turn that into lowercase
set that to a
create l with type number
get length of a
set that to l
create i with type number
set 0 to i
create t with type string
while i < l
get item from a at i
set that to t
get location of t inside "aeiou
if that >= 0
turn t into uppercase
set that to t
endif
output inline t
add i by 1
set that to i
endwhile
UiuaSBCS, 13 bytes
×-1∈"AEIOU".⌵
Happy birthday to Uiua!
Explanation
| desc | stack
×-1∈"AEIOU".⌵ | | "Hello, World!"
⌵ | uppercase | "HELLO, WORLD!"
. | duplicate | "HELLO, WORLD!" "HELLO, WORLD!"
∈"AEIOU" | is vowel? | "HELLO, WORLD!" [0 1 0 0 1 0 0 0 1 0 0 0 0]
-1 | subtract 1 | "HELLO, WORLD!" [¯1 0 ¯1 ¯1 0 ¯1 ¯1 ¯1 0 ¯1 ¯1 ¯1 ¯1]
× | multiply | "hEllO, wOrld!"
Uiua, 14 bytes
¯⍜▽¯∈"AEIOU".⌵
¯⍜▽¯∈"AEIOU".⌵
⌵ # uppercase
⍜▽ ∈"AEIOU". # under keeping the vowels
¯ # lowercase
¯ # flip case
PHP, 45 bytes
fn($s)=>strtr(strtolower($s),"aeiou","AEIOU")
Similar to the other answers here: conversion to lowercase, then the replacement of the vowels aeiou with their capitalized version.
YASEPL, 155 147 145 140 bytes
=f=2)"aeiouAEIOU"=1'=i=l®1`1=k=f=m¥i,1`9!y¥f,2!m}7,y,8!k$`8!+}2,10,9=j`2!j+!q$j»}7,m,2`3!j}1,90,5}2,65,5!$j+32|6`5!$j`6!k[7!-32`7!›!i+}2,l
JS 63 .. 59 Bytes
Hello this is how you can create it in JS using one line of code.
Code:
f=s=>s.toLowerCase().replace(/[aeiou]/g,v=>v.toUpperCase())
Test Cases:
Basic Test Cases:
- Input:
"Hello"- Expected Output:
"hEllO"
- Expected Output:
- Input:
"Stack Exchange"- Expected Output:
"stAck ExchAngE"
- Expected Output:
- Input:
"abcdefghijklmnopqrstuvwxyz"- Expected Output:
"AbcdEfghIjklmnOpqrstUvwxyz"
- Expected Output:
- Input:
Edge Cases:
- Input:
""(Empty string)- Expected Output:
""(Empty string)
- Expected Output:
- Input:
"12345"(No vowels)- Expected Output:
"12345"
- Expected Output:
- Input:
"AEIOU"(All vowels)- Expected Output:
"AEIOU"(All uppercase)
- Expected Output:
- Input:
Case Insensitivity Test:
- Input:
"HeLLoWorLD"- Expected Output:
"hEllOwOrLD"
- Expected Output:
- Input:
Special Characters Test:
- Input:
"@#$%"- Expected Output:
"@#$%"(No vowels, no change expected)
- Expected Output:
- Input:
"hello$world"- Expected Output:
"hEllO$wOrld"(Vowels in the middle of the string)
- Expected Output:
- Input:
Long String Test:
- Input:
"Lorem ipsum dolor sit amet, consectetur adipiscing elit. Sed do eiusmod tempor incididunt ut labore et dolore magna aliqua."- Expected Output:
"lOrEm IpsUm dOlOr sIt AmEt, cOnsEc tEtUr AdIpIscIng ElIt. sEd dO EIUsmOd tEmpOr IncIdIdUnt Ut lAbOrE Et dOlOrE MAgnA AlIqUA."
- Expected Output:
- Input:
Performance Test:
- Input:
"a" repeated 10^6 times (a million times)- Expected Output:
"A"repeated 10^6 times
- Expected Output:
- Input:
"abcdefghijklmnopqrstuvwxyz"repeated 10^4 times- Expected Output:
"AbcdEfghIjklmnOpqrstUvwxyz"repeated 10^4 times
- Expected Output:
- Input:
Random Test Cases:
- Input:
"hElLo!"- Expected Output:
"hEllO!"(Only 'e' should be capitalized)
- Expected Output:
- Input:
"HeLlO 123"- Expected Output:
"hEllO 123"(Only vowels should be capitalized)
- Expected Output:
- Input:
"rAnDoMT3XT"- Expected Output:
"rAndOmt3xt"(Only 'a' and 'o' should be capitalized)
- Expected Output:
- Input:
Try it Online:
f=s=>s.toLowerCase().replace(/[aeiou]/g,v=>v.toUpperCase())
// Test Cases
function runTest(input, expectedOutput) {
const output = f(input);
console.log("Input:", input);
console.log("Expected Output:", expectedOutput);
console.log("Actual Output:", output);
if (output === expectedOutput) {
console.log("Test Passed!");
} else {
console.log("Test Failed!");
}
console.log("-------------------------");
}
// Basic Test Cases
runTest("Hello", "hEllO");
runTest("abcdefghijklmnopqrstuvwxyz", "AbcdEfghIjklmnOpqrstUvwxyz");
// Edge Cases
runTest("", "");
runTest("12345", "12345");
runTest("AEIOU", "AEIOU");
// Case Insensitivity Test
runTest("HeLLoWorLD", "hEllOwOrld");
// Special Characters Test
runTest("@#$%", "@#$%");
runTest("hello$world", "hEllO$wOrld");
// Long String Test
runTest("Lorem ipsum dolor sit amet, consectetur adipiscing elit. Sed do eiusmod tempor incididunt ut labore et dolore magna aliqua.", "lOrEm IpsUm dOlOr sIt AmEt, cOnsEctEtUr AdIpIscIng ElIt. sEd dO EIUsmOd tEmpOr IncIdIdUnt Ut lAbOrE Et dOlOrE mAgnA AlIqUA.");
// Performance Test
runTest("a".repeat(1e6), "A".repeat(1e6));
runTest("abcdefghijklmnopqrstuvwxyz".repeat(1e4), "AbcdEfghIjklmnOpqrstUvwxyz".repeat(1e4));
// Random Test Cases
runTest("hElLo!", "hEllO!");
runTest("HeLlO 123", "hEllO 123");
runTest("rAnDoMT3XT", "rAndOmt3xt");
Scala 3, 94 59 bytes
59 bytes(Saved 35 bytes thanks to @movatica). Attempt This Online!
_.toLowerCase.map(c=>if("aeiou"contains c)c.toUpper else c)
94 bytes. Attempt This Online!
_.toLowerCase.map{case'a'=>'A';case'e'=>'E';case'i'=>'I';case'o'=>'O';case'u'=>'U';case c=>c}
Pip, 9 bytes
LCaRXVUC_
Explanation
LCaRXVUC_
LCa ; Lowercase the input string
R ; Replace
XV ; any lowercase vowel
UC_ ; with itself uppercased
Swift, 95 88 bytes
let c={($0+"").map{"aeiouAEIOU".contains($0) ?$0.uppercased():$0.lowercased()}.joined()}
Uiua SBCS, 14 bytes
¯⍜▽¯∊,"AEIOU"⌵
0.9.0 recently extended abs and negate to work on characters, making this much easier.
¯⍜▽¯∊,"AEIOU"⌵
⌵ # uppercase the input
∊,"AEIOU" # mask of vowels
⍜▽¯ # swap case at indices given by the mask
¯ # swap case
APL(NARS), 45 chars
{⍵∊⎕A∼'AEIOU':⎕a[⎕A⍸⍵]⋄⍵∊'aeiou':⎕A[⎕a⍸⍵]⋄⍵}¨
test & how to use:
f←{⍵∊⎕A∼'AEIOU':⎕a[⎕A⍸⍵]⋄⍵∊'aeiou':⎕A[⎕a⍸⍵]⋄⍵}¨
f 'Hello World'
hEllO wOrld
Swift, 125 bytes
Short version:
func c(_ t:String)->String{return t.lowercased().map({String($0)}).map({"aeiou".contains($0) ?$0.uppercased():$0}).joined()}
Verbose version:
func c2 (_ t: String) -> String {
let s = t.map({String($0).lowercased()})
let lv = ["a", "e", "i", "o", "u"]
let r = s.map { l in
lv.contains(l) ? l.uppercased() : l
}.joined()
return r
}
Gema, 36 characters
/[aeiou]/=@upcase{$0}
?=@downcase{?}
Sample run:
bash-5.2$ gema '/[aeiou]/=@upcase{$0};?=@downcase{?}' <<< 'Hello World'
hEllO wOrld
Vyxal s, 35 bitsv2, 4.375 bytes
⇩ƛAß⇧
Bitstring:
11010011100001101111110111101101010
minus 6 bits thanks to pacmanboss256
Posted as a separate answer because a) vyncode isn't a golfing suggestion and b) it's different enough to be its own answer - it's not a byte shave from the other answer.
Explained (old)
ƛA[⇧|⇩
ƛ # To each character:
A[ # if it is a vowel:
⇧ # uppercase
| # else:
⇩ # lowercase
Thunno 2 J, 8 bytes
ıDṃh?R:L
Explanation
ıDṃh?R:L # Implicit input
ı # Map over each character
D # Duplicate the current character
ṃh? # If it's a vowel:
R # Uppercase it
: # Otherwise:
L # Lowercase it
# Implicit output, joined into a string
Zsh --extendedglob, 39
<<<${${(L)@}//(#m)[aeiou]/${(U)MATCH/}}
Code borrowed from the Zsh Manual, Globbing Flags
Python 3, 68 bytes
lambda s:''.join([p.upper()if p in'aeiou'else p for p in s.lower()])
Vyxal, 7 bytes
ɽƛAß⇧;ṅ
ɽƛAß⇧;ṅ # Takes the string as input
ɽ # Lowercase the string
ƛ ; # Map the characters in the string to...
A # Is character a vowel?
ß⇧ # If truthy, uppercase the letter
ṅ # Join by nothing
Excel VBA, 78 75 bytes
Saved 3 bytes thanks to Taylor Raine because of course
t=LCase([A1]):For Each c in Split("A E I O U"):t=Replace(t,c,c,,,1):Next:?t
Code is run in the immediate window and uses the cell A1 from the active sheet as it's input. The method is simple:
- Save the lowercase version of the input as
t - Save the vowels as
s - For each vowel, replace the lowercase version with the uppercase version
- Print the result
Brachylog, 10 9 bytes
ḷ{∈Ṿ&ụ|}ᵐ
-1 byte thanks to @DLosc.
Explanation
ḷ Lowercase the whole input
{ }ᵐ Map on each char of the input:
∈Ṿ The char lowercased is in "aeiou"
&ụ Output the char uppercased
| Or do nothing
Vim, 19 bytes
Vu:s/[aeiou]/\U&/g
Explanation
Vu
Select the current line and lowercase it.
:s/
In the current line, replace...
[aeiou]/
a vowel character with...
\U&/
the uppercase version of itself...
g<nl>
for every match on the line.
Lexurgy, 111 bytes
Simple replacement
a:
{a,e,i,o,u,B,C,D,F,G,H,J,K,L,M,N,P,Q,R,S,T,V,W,X,Y,Z}=>{A,E,I,O,U,b,c,d,f,g,h,j,k,l,m,n,p,q,r,s,t,v,w,x,y,z}
APL (Dyalog Unicode), 17 bytes SBCS
1⎕C@(∊∘'aeiou')⎕C
A tacit function that ports @Adam's Dyalog Extended solution into vanilla Dyalog APL. Makes use of the system function ⎕C introduced in Dyalog 18.0 (hence, won't work in TIO).
1⎕C@(∊∘'aeiou')⎕C ⍝ Tacit function.
⎕C ⍝ Convert to lowercase. Then:
1⎕C ⍝ Convert to uppercase
@ ⍝ at the positions
(∊∘'aeiou') ⍝ that are part of the vowels.
Pyth, 20 bytes
K"aeiou"smrd}d+KrK1w
Explanation
w=input()
K"aeiou" K="aeiou"
s "".join(
m map(
r lambda d:r(
d d,
d d
} in
K K
+ +
rK1 K.upper())),
w w)
Funky2, 57 bytes
s=>s:gsub("."c=>ifc:match"[aeiou]"c:upper()elsec:lower())
Relatively simple solution, benefits from Funky2's relatively concise language.
SML 65 bytes
map(fn c=>if contains"AEIOUaeiou"c then toUpper c else toLower c)
This requires open String and open Char. If those are not allowed, then it would be 87 bytes (String.map Char.contains Char.toUpper Char.toLower).
Python 3, 82 bytes
f=lambda c:c.upper()if c in 'aeiou'else c.lower();lambda s:''.join(map(f,list(s)))
K (oK), 27 23 21 bytes
Solution:
`c$x-32*~^"aeiou"?x:_
Explanation:
Even nicer approach thanks to @ngn... either substract 0 or 32 from the input based on whether or not it's a vowel:
`c$x-32*~^"aeiou"?x:_ / the solution
_ / lowercase input
x: / store as 'x'
"aeiou"? / lookup input in "aeiou" else null
^ / is null?
~ / not
-32* / multiply boolean list by -32 (yields 0 or -32)
x / subtract from x
`c$ / cast back to characters
Notes:
- -6 bytes thanks to @ngn
K4, 24 bytes
Solution:
.q.ssr/[;_v;v:"AEIOU"]@_
Explanation:
Replace each lowercase vowel with the uppercase equivalent.
.q.ssr/[;_v;v:"AEIOU"]@_ / the solution
_ / lowercase
@ / apply
.q.ssr/[; ; ] / search/replace, iterate over vowels
v:"AEIOU" / store uppercase vowels as 'v'
_v / lowercase vowels
Python 3, 50 bytes
lambda s:bytes(c^(c^~68174912>>c%32)&32for c in s)
Port of Arnauld's JS answer using bytes object in Python. Because Python's >> does not imply %32 on its right argument, it must be done manually.
Python 3, 70 bytes
lambda s:''.join(l.upper()if l in'aeiouAEIOU'else l.lower()for l in s)
JavaScript (V8), 93...77 bytes
Log:
- -5 bytes thanks to @Benji
- -14 bytes: switched to case insensitive matching and
.test()
s=>[...s].map(c=>/[aeiou]/i.test(c)?c.toUpperCase():c.toLowerCase()).join('')
s=> // es6 arrow function
[...s]. // split input string into array
map(c => // another es6 arrow function, this time for a callback iterating over the array
/[aeiou]/i // case insensitive regex
.test(c)? // use a ternary operator to check if the character matches the regex
c.toUpperCase(): // if true return character to uppercase
c.toLowerCase()) // otherwise return lowercase
.join('') // join the array back into a string
Methods mentioned:
Haskell, 68 bytes
import Data.Char
f=map(g.toLower)
g x|x`elem`"aeiou"=toUpper x|1<2=x
Python, 53 bytes
lambda s:[(c*2).title().strip('aeiou')[-1]for c in s]
Outputs a list of characters.
Here is an explanation of how it transforms each character c, with examples c='A' and c='B':
'A' 'B'
(c*2) 'AA' 'BB' # Two copies of c in a two-character string
.title() 'Aa' 'Bb' # Title case: uppercase first letter and the rest lowercase
# This disregards the case of c
.strip("aeiou") 'A' 'Bb' # Remove any leading or trailing lowercase vowels 'aeiou'
# For a two-character string, this removes all such letters
[-1] 'A' 'b' # Take the last letter
55 bytes
lambda s:[(c*2).title()[~(c in'aieouAEIOU')]for c in s]
If we were instead lowercasing vowels and uppercasing consonants, we wouldn't need the ~() and would have 52 bytes.
Python 3, 85 84 bytes
x=''
for i in input():
if i in'aAeEiIoOuU':x+=i.upper()
else:x+=i.lower()
print(x)
Pretty new to codegolf, so probably not the shortest solution
edit 1: one byte saved because of a space
Javascript (V8), 97 bytes
s=>[...s].map(c=>{t="aeiouAEIOU";return(t.includes(c)?c.toUpperCase():c.toLowerCase())}).join('')
Takes a string, iterate over every chars and check if the char is a vowel. If so return the char in uppercase, otherwise in lowercase. Then join the return of the map with a void char.
- First post/answer here, if you see some errors or missed case, or just want to improve my code feel free to do so
Python 3, 55 bytes
lambda s:[[c,c.upper()][c in"aeiou"]for c in s.lower()]
Input: A string/sequence of characters
Output: a list of characters.
Explanation
The solution converts the string to lower case, then convert all vowels to uppercase.
for c in s.lower()converts the string to lower case, then loop through each character in the string.[c,c.upper()][c in "aeiou"]converts any vowel to uppercase, and consonant to lower case.
c in "aeiou"evaluates to0or1, which is used to index into the list[c,c.upper()].
T-SQL, 48 bytes
SELECT TRANSLATE(LOWER(v),'aeiou','AEIOU')FROM t
Takes input from a pre-existing table t with varchar column v, per our IO rules.
Converts the entire string to lowercase, then makes just the vowels uppercase. The function TRANSLATE works in SQL 2017 and later.
Perl 5 -p, 20 19 bytes
-1 due to @NahuelFouilleul
$_=lc;y;aeiou;AEIOU
Convert everything to lower case, then change vowels to upper case.
Bash + Core utilities, 25 23 bytes
tr aeiou AEIOU<<<${1,,}
Saved 2 bytes thanks to a suggestion of Nahuel Fouilleul.
Input is passed as an argument, output is on stdout.
Ruby, 33 bytes
->s{s.downcase.tr"aeiou","AEIOU"}
Straightforward solution: downcase everything, then upcase vowels.
C (gcc), 49 bytes
Port of my JS answer.
f(char*s){for(;*s;s++)*s^=(*s^~68174912>>*s)&32;}
C (clang), 48 bytes
A version suggested by @Neil
This is abusing the way clang is dealing with the pointer post-increment.
f(char*s){for(;*s;)*s++^=(*s^~68174912>>*s)&32;}
Batch, 118 115 Bytes
3 bytes saved thanks to Neil
@Set o=%*
@For %%A in (A E I O U b c d f g h j k l m n p q r s t v w x y z)do @Call Set o=%%o:%%A=%%A%%
@ECHO(%O%
Explanation:
Uses Call Set to update variable during operation of For Loop in conjunction with Substring Modification: VarName=%VarName:ToSub=SubValue%.
Substring modification is not case-sensitive - case is determined using the defined For loop set %%A in (set)
x86-64 machine code, 21 bytes
(Or 20 bytes for an x86-32 version with an explicit length input, allowing dec/jnz as the loop condition. Using cl for a shift count makes it not a win to use loop, and 64-bit mode has 2-byte dec so it's break-even to make it explicit-length).
Callable as void vucd_implicit(char *rdi) with the x86-64 System V calling convention. (It leaves RDI pointing to the terminating 0 byte if you want to use that bonus return value.)
# disassembly: objdump -drwC -Mintel
0000000000401000 <theloop>:
401000: b8 a0 bb ef fb mov eax,0xfbefbba0
401005: d3 e8 shr eax,cl
401007: 30 c8 xor al,cl
401009: 24 20 and al,0x20
40100b: 30 c8 xor al,cl
40100d: aa stos BYTE PTR es:[rdi],al
000000000040100e <vowel_up_consonant_down>: # the function entry point
40100e: 8a 0f mov cl,BYTE PTR [rdi]
401010: 84 c9 test cl,cl
401012: 75 ec jne 401000 <theloop>
401014: c3 ret
Notice that the function entry point is in the middle of the loop. This is something you can do in real life; as far as other tools are concerned, theloop is another function that falls into this one as a tailcall.
This uses something like Arnauld's xor/and/xor idea for applying the lcase bit to an input character, instead of the more obvious and cl, ~0x20 to clear it in the original, and al, 0x20 to isolate it from the mask, and or al, cl to combine. That would be 1 byte larger because and cl, imm8 can't use the AL,imm special encoding with no ModRM.
Having the bitmap left-shifted by 5 so the bit we want lines up with 0x20 is also due to @Arnauld's answer. I had been planning to use bt/salc like in a previous vowel/consonant bitmap answer and mask that with 0x20 until I tried Arnauld's way and found it could be done even more efficiently.
NASM source (Try it online! with a test caller that does strlen on a command line arg and uses a write() system call afterward)
global vowel_up_consonant_down
theloop:
; consonant bitmap
; ZYXWVUTSRQPONMLKJIHGFEDCBA@ For indexing with ASCII c&31 directly
mov eax, 111110111110111110111011101b << 5 ; line up with the lcase bit
; the low bit is 1 to preserve 0x20 ' '
shr eax, cl ; AL & 0x20 is how the lowercase bit *should* be set
xor al, cl ; bitdiff = (mask>>c) & c
and al, 0x20 ; isolate the lowercase bit
xor al, cl ; flip the lcase bit if needed
stosb ; and store
vowel_up_consonant_down:
mov cl, [rdi]
test cl, cl
jnz theloop ; }while(c != 0)
ret
Variants
no spaces: 19 bytes
If we didn't need to handle spaces (ASCII 0x20), we enter the function at the top, with the mov cl, [rdi] load at the top, but still leave the loop condition at the bottom. So we'd load and re-store the terminating 0, and the XOR that produced it would set ZF. The low bit of the bitmap would be 0 instead of 1.
vucd_pure_alphabetic:
.loop:
mov cl, [rdi]
... ; same, but with bitmap[0] => 0
xor al,cl
jnz .loop ; mask>>0 leave the terminating 0 unmodified; xor sets ZF
Upper-case-only input, like the A to Z in the question indicates: 19 bytes
(Or 17 without spaces either.)
If we can assume the lower-case bit was already cleared on input ASCII bytes, we can save one XOR (and change the other one to an OR)
...
shr eax, cl
and al, 0x20
or al, cl
...
Using the bt instruction:
Normally testing a bitmap is a job for the bt instruction, but where we're not branching on the result, it turns out to be cheaper to shift it, even though that means we can't easily use the loop instruction. (I haven't gone back to this idea to re-golf it after realizing we need to handle spaces).
I suspect there's room for more golfing, but the first version of this I tried was
vucd:
.loop:
mov dl, [rdi]
; ZYXWVUTSRQPONMLKJIHGFEDCBA@ 1-indexed using ASCII codes directly
mov esi, 111110111110111110111011101b ; consonant/vowel bitmap for use with bt
bt esi, edx ; CF = mask & (1U << (c&31))
%if CPUMODE == 32
salc ; 1B only sets AL = 0 or 0xFF. Not available in 64-bit mode
%else
sbb eax, eax ; 2B eax = 0 or -1, according to CF.
%endif
xor al, dl
and al, 0x20 ; just the lowercase bit
xor al, dl
loop .loop
ret
Not re-tested after tweaking to handle spaces.
bt + salc in 32-bit mode costs the same as shr reg,cl + the extra test cl,cl that's needed because we can't use loop. So I think this is also 21 bytes. But 32-bit mode explicit-length can just dec/jnz a reg other than cl for a 20-byte total.
mov esi, imm32 can be hoisted out of the loop, or we can use EAX. Neither affects byte count, only efficiency or the calling-convention.
Java 8, 86 34 bytes
S->S.map(c->c^(c^~68174912>>c)&32)
-52 bytes by porting @Arnauld's JavaScript answer, so make sure to upvote him!!
Original 86 bytes answer:
s->{s=s.toLowerCase();for(var p:"aeiou".toCharArray())s=s.replace(p,p&=~32);return s;}
Explanation:
s->{ // Method with String as both parameter and return-type
s=s.toLowerCase(); // Convert the entire String to lowercase
for(var p:"aeiou".toCharArray()) // Loop over the vowels as characters:
s=s.replace(p,p&=~32); // And replace the lowercase vowels to uppercase ones
return s;} // Then return the modified String as result
JavaScript (Node.js), 55 ... 46 45 bytes
Saved 1 byte thanks to @KevinCruijssen
s=>Buffer(s).map(c=>c^(c^~68174912>>c)&32)+''
How?
The constant \$68174912\$ is a bitmask describing the positions of the vowels:
00000100000100000100010001000000
v v v v v
zyxwvutsrqponmlkjihgfedcba`_^]\[
As per the ECMAScript specification, the following expression:
~68174912 >> c & 32
is equivalent to:
~68174912 >> (c % 32) & 32
and therefore evaluates to \$32\$ for a consonant or \$0\$ for a vowel, no matter the case of \$c\$.
Commented
s => // s = input string
Buffer(s) // turn s into a buffer
.map(c => // for each ASCII code c:
c ^ // change the case if:
( c // c is not in lower case
^ // XOR
~68174912 >> c // c is a consonant
) & 32 //
) + '' // end of map(); coerce back to a string
x86 platform-independent machine code, 46 bytes.
Expects the string pointer to be passed in eax, trashes ebx and edx.
Entry point is located at 0x26.
Hex dump:
BA 22 82 20 00 D3 EA F6 C2 01 74 0B 8D
51 9F 83 FA 19 8D 59 E0 EB 09 8D 51 BF
83 FA 19 8D 59 20 0F 46 CB 88 08 40 0F
BE 08 85 C9 75 D3 C3
Disassembly:
00000000 BA22822000 mov edx,0x208222
00000005 D3EA shr edx,cl
00000007 F6C201 test dl,0x1
0000000A 740B jz 0x17
0000000C 8D519F lea edx,[ecx-0x61]
0000000F 83FA19 cmp edx,byte +0x19
00000012 8D59E0 lea ebx,[ecx-0x20]
00000015 EB09 jmp short 0x20
00000017 8D51BF lea edx,[ecx-0x41]
0000001A 83FA19 cmp edx,byte +0x19
0000001D 8D5920 lea ebx,[ecx+0x20]
00000020 0F46CB cmovna ecx,ebx
00000023 8808 mov [eax],cl
00000025 40 inc eax
00000026 0FBE08 movsx ecx,byte [eax]
00000029 85C9 test ecx,ecx
0000002B 75D3 jnz 0x0
0000002D C3 ret
byte count = 0x2E = 46
Red, 83 82 bytes
func[s][v: charset"aoeiu"parse lowercase s[any[to v change t: v(uppercase t/1)]]s]
W d, 9 bytes
→d╧o╧╪0╨←
Uncompressed:
("aeiou":)Z
Explanation
( % Convert to lowercase
"aeiou" % All vowel strings
: % Duplicate
) % Convert to uppercase
Z % Transliterate
Io, 72 bytes
Takes input as a special format.
method(x,x map(i,if("aeiou"containsSeq(i),i asUppercase,i asLowercase)))
sed, 41 25 24 bytes
Saved 16 bytes thanks to Surculose Sputum!!!
Saved a byte thanks to S.S. Anne!!!
s/./\L&/g
y/aeiou/AEIOU/
C (gcc), 58 \$\cdots\$ 58 bytes
f(char*s){for(;*s;)*s++=index("aeiou",*s|32)?*s&95:*s|32;}
I tried to find a pattern in the vowels' representations using the modulo operator but nothing short enough. Instead, use strchr.
Fixed a bug kindly pointed out by Noodle9 at the cost of 3 bytes.
-1 byte thanks to Noodle9!
-1 byte thanks to Surculose Sputum!
-1 byte thanks to ceilingcat!
C (gcc), 75 \$\cdots\$72 71 bytes
Added 4 bytes to fixed a bug.
Saved a byte thanks to ceilingcat!!!
u;f(char*s){for(;*s;*s++=u-65&&u-69&&u-73&&u-79&&u-85?*s|32:u)u=*s&95;}
Charcoal, 14 bytes
⭆↧S⎇№aeiouι↥ιι
Try it online! Link is to verbose version of code. Explanation:
S Input string
↧ Lowercased
⭆ Map over characters and join
aeiou Literal string `aeiou`
№ Count occurrences of
ι Current character
⎇ If non-zero
ι Current character
↥ Uppercased
ι Else current character
Implicitly print
Retina, 9 bytes
T`vVL`VVl
Try it online! Explanation:
T`v`V
Lowercase vowels get uppercased.
T`V`V
Uppercase vowels also get uppercased, to avoid being matched later.
T`L`l
All other uppercase letters get lowercased.
APL (Dyalog Extended), 14 bytesSBCS
Anonymous tacit prefix function.
⌈@(∊∘'aeiou')⌊
⌊ lowercase
⌈@(…) uppercase at the following positions:
∊∘'aeiou' members of "aeiou"
CJam, 15 bytes
lel"aeiou"_euer
Explanation
l e# Read line
el e# To lowercase
"aeiou" e# Push this string
_ e# Duplicate
eu e# To uppercase
er e# Transliterate. Implicitly display
05AB1E, 9 6 bytes
Saved 3 using Luis Mendo's CJam approach
lžMDu‡
Try it online! (Footer formats the resulting list of characters as a plain string)
How?
lžMDu‡ e.g. input="LowEr" stack: []
l - push lower-case (input) ["lower"]
žM - push lower-case vowels ["aeiou", "lower"]
D - duplicate ["aeiou", "aeiou", "lower"]
u - upper-case ["AEIOU", "aeiou", "lower"]
‡ - transliterate ["lOwEr"]
- implicit print lOwEr
Jelly, 9 7 bytes
Saved 2 using a variation of Luis Mendo's CJam approach
ØCŒHyŒu
A monadic Link accepting a list of characters which yields a list of characters.
How?
ØCŒHyŒu - Link: list of characters, S e.g. "I am OK!"
ØC - consonants "BCDF...XYZbcdf...xyz"
ŒH - split into two ["BCDF...XYZ", "bcdf...xyz"]
Œu - convert (S) to upper-case "I AM OK!"
y - translate "I Am Ok!"

