30
\$\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
  • \$\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

51 Answers 51

1
2
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\$
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\$

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

Vyxal s, 41 bits1, 5.125 bytes

ƛA[⇧|⇩

Try it Online!

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

ƛA[⇧|⇩
ƛ       # To each character:
 A[     #   if it is a vowel:
   ⇧    #     uppercase
    |   #   else:
     ⇩  #     lowercase
\$\endgroup\$
0
\$\begingroup\$

Lua, 64 bytes

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

Try it online!

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

Python 3, 82 bytes

f=lambda c:c.upper()if c in 'aeiou'else c.lower();lambda s:''.join(map(f,list(s)))
\$\endgroup\$
1
2

Your Answer

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

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