35
\$\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
    Commented 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
    Commented 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
    Commented Feb 16, 2022 at 18:44

66 Answers 66

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

jBasher2, 437 bytes

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
create o with type string
while i < l
get item from a at i
set that to t
get location of t inside "aeiou
set that to o
spawn t
if o >= 0
turn t into uppercase
endif
output inline that
add i by 1
set that to i
endwhile
\$\endgroup\$
0
\$\begingroup\$

Uiua, 14 bytes

¯⍜▽¯∈"AEIOU".⌵

Try it!

¯⍜▽¯∈"AEIOU".⌵
              ⌵ # uppercase
 ⍜▽ ∈"AEIOU".   # under keeping the vowels
    ¯           # lowercase
¯               # flip case
\$\endgroup\$
1
  • \$\begingroup\$ Use ⍥¯ for -1 \$\endgroup\$ Commented Nov 29 at 23:49
-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
3

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.