34
\$\begingroup\$

Challenge

You will be given a string that can contain lowercase letters, uppercase letters, or spaces. You have to turn the vowels (a, e, i, o, u) in the string to upper case and consonants to lower case. This applies whether or not the letter was originally upper case or lower case. Spaces remain as is. Note that "y" is a consonant.

Examples

Hello World -> hEllO wOrld
abcdefghijklmnopqrstuvwxyz ABCDEFGHIJKLMNOPQRSTUVXWYZ -> AbcdEfghIjklmnOpqrstUvwxyz AbcdEfghIjklmnOpqrstUvxwyz

Info

  • The input string can contain letters A to Z, lowercase or uppercase, and spaces.

Input

The string

Output

The formatted string (Vowels uppercase and consonants lowercase).

\$\endgroup\$
3
  • 1
    \$\begingroup\$ Why on many challenges is "y" counted as a consonant ? \$\endgroup\$
    – CreaZyp154
    Feb 16, 2022 at 13:40
  • \$\begingroup\$ Because it is a consonant. (<pedantic> unless you're using international phonetic alphabet, where it's the front-high-rounded vowel /y/</pedantic>) \$\endgroup\$
    – bigyihsuan
    Feb 16, 2022 at 15:35
  • \$\begingroup\$ @bigyihsuan It's a bit more complicated than that. Wikipedia says: "In the English writing system, it mostly represents a vowel and seldom a consonant." As a child, I was taught that the vowels were "A, E, I, O, U, and sometimes Y," which could lead to viewing Y as "not really a vowel" because it's not always a vowel. Y is also a consonant in some very common words like "yes" and "you." But these are just guesses; I'm not entirely sure why Y is often not considered a vowel in coding challenges. \$\endgroup\$
    – DLosc
    Feb 16, 2022 at 18:44

62 Answers 62

2
\$\begingroup\$

Python 3, 68 bytes

lambda s:''.join([p.upper()if p in'aeiou'else p for p in s.lower()])

Try it online!

\$\endgroup\$
2
\$\begingroup\$

Zsh --extendedglob, 39

<<<${${(L)@}//(#m)[aeiou]/${(U)MATCH/}}

Try it Online

Code borrowed from the Zsh Manual, Globbing Flags

\$\endgroup\$
2
\$\begingroup\$

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}
\$\endgroup\$
1
1
\$\begingroup\$

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
\$\endgroup\$
1
\$\begingroup\$

W d, 9 bytes

→d╧o╧╪0╨←

Uncompressed:

("aeiou":)Z

Explanation

(           % Convert to lowercase
 "aeiou"    % All vowel strings
        :   % Duplicate
         )  % Convert to uppercase
          Z % Transliterate
\$\endgroup\$
1
\$\begingroup\$

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)

No TIO Available

\$\endgroup\$
1
  • 1
    \$\begingroup\$ Given that the input is limited to letters and spaces, could you use @set o=%*? \$\endgroup\$
    – Neil
    Mar 23, 2020 at 11:18
1
\$\begingroup\$

05AB1E, 8 7 6 bytes

-1 byte thanks to Kevin Cruijssen

lžODu‡

Try it online!

\$\endgroup\$
5
  • 1
    \$\begingroup\$ Fails when there are upper-case vowels present in the input \$\endgroup\$ Mar 22, 2020 at 17:09
  • 1
    \$\begingroup\$ Also not sure why you're treating y as a vowel, usually it is not considered a vowel. \$\endgroup\$ Mar 22, 2020 at 17:14
  • \$\begingroup\$ @JonathanAllan thanks, fixed the bug with uppercase vowels. Regarding your second comment, y is sometimes not considered a vowel, but I sure wouldn’t say that’s “usual”. Y is the sixth vowel letter according to Wikipedia. \$\endgroup\$
    – Grimmy
    Mar 22, 2020 at 20:54
  • 1
    \$\begingroup\$ You can remove the surrounding map: try it online. \$\endgroup\$ Mar 23, 2020 at 12:31
  • \$\begingroup\$ @KevinCruijssen thanks! \$\endgroup\$
    – Grimmy
    Mar 23, 2020 at 12:39
1
\$\begingroup\$

Python 3, 85 84 bytes

x=''
for i in input():
 if i in'aAeEiIoOuU':x+=i.upper()
 else:x+=i.lower()
print(x)

Try it online!

Pretty new to codegolf, so probably not the shortest solution

edit 1: one byte saved because of a space

\$\endgroup\$
1
  • \$\begingroup\$ Something like this gets you 75 bytes, but judging by other Python answers, there are better approaches... \$\endgroup\$
    – mkst
    May 23, 2020 at 14:57
1
\$\begingroup\$

Python 3, 70 bytes

lambda s:''.join(l.upper()if l in'aeiouAEIOU'else l.lower()for l in s)

Try it online!

\$\endgroup\$
0
1
\$\begingroup\$

SML 65 bytes

Try it!

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

\$\endgroup\$
2
  • \$\begingroup\$ You can open the libraries, but then those calls are part of the code and should be counted. Thus, in this case it is best to open only Char but use String.map for 82 bytes: Try it online! \$\endgroup\$
    – Laikoni
    Feb 15, 2021 at 16:07
  • \$\begingroup\$ 78 bytes by using this tip: Try it online! \$\endgroup\$
    – Laikoni
    Feb 15, 2021 at 16:09
1
\$\begingroup\$

Funky2, 57 bytes

s=>s:gsub("."c=>ifc:match"[aeiou]"c:upper()elsec:lower())

Relatively simple solution, benefits from Funky2's relatively concise language.

Attempt This Online!

\$\endgroup\$
1
\$\begingroup\$

APL (Dyalog Unicode), 17 bytes SBCS

1⎕C@(∊∘'aeiou')⎕C

Try it on APLgolf!

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.
\$\endgroup\$
1
\$\begingroup\$

Vim, 19 bytes

Vu:s/[aeiou]/\U&/g

Try it online!

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.

\$\endgroup\$
1
\$\begingroup\$

Brachylog, 10 9 bytes

ḷ{∈Ṿ&ụ|}ᵐ

Try it online!

-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
\$\endgroup\$
0
1
\$\begingroup\$

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

Screenshot

\$\endgroup\$
1
  • \$\begingroup\$ You can use a For each loop to get this a bit smaller- t=[Lower(A1)]:For Each c in Split("A E I O U"):t=Replace(t,c,c,,,1):Next:?t for 75 bytes \$\endgroup\$ Feb 22, 2022 at 2:26
1
+100
\$\begingroup\$

Vyxal, 7 bytes

ɽƛAß⇧;ṅ

Try it Online!

ɽƛ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
\$\endgroup\$
1
  • \$\begingroup\$ -1 byte: ɽ⟑Aß⇧₴ \$\endgroup\$
    – naffetS
    Dec 19, 2022 at 2:05
1
\$\begingroup\$

Arturo, 43 bytes

$=>[replace lower&split"aeiou"split"AEIOU"]

Try it

\$\endgroup\$
1
\$\begingroup\$

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
\$\endgroup\$
1
\$\begingroup\$

Vyxal s, 35 bitsv2, 4.375 bytes

⇩ƛAß⇧

Try it Online!

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
\$\endgroup\$
1
  • \$\begingroup\$ 4.375 \$\endgroup\$
    – pacman256
    Dec 25, 2023 at 0:27
1
\$\begingroup\$

R, 45 bytes

function(s)chartr('aeiou','AEIOU',tolower(s))

Try it online!

\$\endgroup\$
1
\$\begingroup\$

Swift, 95 88 bytes

let c={($0+"").map{"aeiouAEIOU".contains($0) ?$0.uppercased():$0.lowercased()}.joined()}
\$\endgroup\$
1
\$\begingroup\$

Pip, 9 bytes

LCaRXVUC_

Attempt This Online!

Explanation

LCaRXVUC_
LCa        ; Lowercase the input string
   R       ; Replace
    XV     ;  any lowercase vowel
      UC_  ;  with itself uppercased
\$\endgroup\$
1
\$\begingroup\$

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:

  1. Basic Test Cases:

    • Input: "Hello"
      • Expected Output: "hEllO"
    • Input: "Stack Exchange"
      • Expected Output: "stAck ExchAngE"
    • Input: "abcdefghijklmnopqrstuvwxyz"
      • Expected Output: "AbcdEfghIjklmnOpqrstUvwxyz"
  2. Edge Cases:

    • Input: "" (Empty string)
      • Expected Output: "" (Empty string)
    • Input: "12345" (No vowels)
      • Expected Output: "12345"
    • Input: "AEIOU" (All vowels)
      • Expected Output: "AEIOU" (All uppercase)
  3. Case Insensitivity Test:

    • Input: "HeLLoWorLD"
      • Expected Output: "hEllOwOrLD"
  4. Special Characters Test:

    • Input: "@#$%"
      • Expected Output: "@#$%" (No vowels, no change expected)
    • Input: "hello$world"
      • Expected Output: "hEllO$wOrld" (Vowels in the middle of the string)
  5. 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."
  6. Performance Test:

    • Input: "a" repeated 10^6 times (a million times)
      • Expected Output: "A" repeated 10^6 times
    • Input: "abcdefghijklmnopqrstuvwxyz" repeated 10^4 times
      • Expected Output: "AbcdEfghIjklmnOpqrstUvwxyz" repeated 10^4 times
  7. Random Test Cases:

    • Input: "hElLo!"
      • Expected Output: "hEllO!" (Only 'e' should be capitalized)
    • Input: "HeLlO 123"
      • Expected Output: "hEllO 123" (Only vowels should be capitalized)
    • Input: "rAnDoMT3XT"
      • Expected Output: "rAndOmt3xt" (Only 'a' and 'o' should be capitalized)

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

\$\endgroup\$
2
  • \$\begingroup\$ Nice answer, and welcome. You can remove all of the spaces in your code. Also, iirc in JS, it'll still compile/run even without the leading const and trailing semi-colon, so it can be f=s=>s.toLowerCase().replace(/[aeiou]/g,v=>v.toUpperCase()) (59 bytes). \$\endgroup\$ Mar 4 at 13:43
  • \$\begingroup\$ Thank you very much for your prompt reply. \$\endgroup\$
    – Adam Basha
    Mar 4 at 13:55
0
\$\begingroup\$

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}
\$\endgroup\$
0
\$\begingroup\$

Lua, 64 bytes

print(((...):lower():gsub('[aeiou]',load'return(...):upper()')))

Try it online!

\$\endgroup\$
0
\$\begingroup\$

Vyxal 3 s, 5 bytes

ʀƛA¿ɾ

Try it Online!

lowercase the entire string then uppercase the vowels

\$\endgroup\$
0
\$\begingroup\$

Gema, 36 characters

/[aeiou]/=@upcase{$0}
?=@downcase{?}

Sample run:

bash-5.2$ gema '/[aeiou]/=@upcase{$0};?=@downcase{?}' <<< 'Hello World'
hEllO wOrld

Try it online!

\$\endgroup\$
0
\$\begingroup\$

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
}
\$\endgroup\$
0
\$\begingroup\$

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
\$\endgroup\$
0
\$\begingroup\$

Uiua SBCS, 14 bytes

¯⍜▽¯∊,"AEIOU"⌵

Try it!

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
\$\endgroup\$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.