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

37 Answers 37

1
2
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\$ – streetster May 23 '20 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\$

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

K (oK), 27 23 21 bytes

Solution:

`c$x-32*~^"aeiou"?x:_

Try it online!

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
\$\endgroup\$
4
  • 1
    \$\begingroup\$ hint: (-32*isvowel)+x leads to a shorter solution than @[;;;] \$\endgroup\$ – ngn May 23 '20 at 8:45
  • \$\begingroup\$ i was thinking of `c$(-32*x in"aeiou")+x:_ but yours is even better :) \$\endgroup\$ – ngn May 23 '20 at 15:36
  • 1
    \$\begingroup\$ -2: `c$x-32*~^"aeiou"?x:_ \$\endgroup\$ – ngn May 23 '20 at 15:40
  • \$\begingroup\$ Thanks - I managed to golf another byte as I was writing up the explanation - let me update with your latest :) \$\endgroup\$ – streetster May 23 '20 at 19:11
1
\$\begingroup\$

Julia 49 bytes

Try it!

f=l->map(c->in(c,"aeiou") ? c-32 : c,lowercase.(l))
\$\endgroup\$
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 at 16:07
  • \$\begingroup\$ 78 bytes by using this tip: Try it online! \$\endgroup\$ – Laikoni Feb 15 at 16:09
-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, privacy policy and cookie policy

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