-6
\$\begingroup\$

Description

Your task is to take a string as input, and convert the first letter of each word to uppercase, and the rest to lowercase.

Test Cases

Input => Output

ISN'T THIS A REALLY COOL TEST CASE??? => Isn't This A Really Cool Test Case???
tHis iS A rEALLY WeiRd SeNTEncE => This Is A Really Weird Sentence
I Like This Sentence => I Like This Sentence

Edit: no built-ins allowed.


This is , so lowest byte count wins. Good luck!

\$\endgroup\$

closed as unclear what you're asking by Mego, DJMcMayhem, xnor, Kevin Cruijssen, Dennis Jun 20 '16 at 6:49

Please clarify your specific problem or add additional details to highlight exactly what you need. As it's currently written, it’s hard to tell exactly what you're asking. See the How to Ask page for help clarifying this question. If this question can be reworded to fit the rules in the help center, please edit the question.

  • 3
    \$\begingroup\$ Since at least one current answer uses built-ins, I'd like to ever so subtly drop this link here in the comments. \$\endgroup\$ – Geobits Jun 20 '16 at 5:35
  • 8
    \$\begingroup\$ What counts as a built-in? \$\endgroup\$ – Leaky Nun Jun 20 '16 at 5:38
  • 7
    \$\begingroup\$ How do you define a word? \$\endgroup\$ – Dennis Jun 20 '16 at 6:00
  • 4
    \$\begingroup\$ Possible duplicate of Capitalize first letter of each word of input \$\endgroup\$ – Kevin Cruijssen Jun 20 '16 at 6:48
  • 4
    \$\begingroup\$ @Geobits I disagree. When there's only a few answers yet, improving the challenge is more important than avoiding the invalidation of a few answers. Of course when a rule change would require rewriting 20 answers, then it's probably not worth it, but this early on, I'm definitely n favour of the change. Nevertheless, MCMastery, I'd recommend using the sandbox next time where details like this can be ironed out before you risk invalidating answers. \$\endgroup\$ – Martin Ender Jun 20 '16 at 7:03
3
\$\begingroup\$

V, 14 8 bytes

gu$ògUlW

Try it online!

Explanation:

gu$       #Make everything lowercase
   ò      #Recursively
    gUl   #Make the current character uppercase
       W  #And move forward a word

Here is a 9-byte, non-competing version (the old interpreter had a bug):

gu$Ó¼÷/Õ°

This version is just a regex:

gu$            #Make everything lowercase
   Ó¼÷/Õ°      #Expands into ':s/\<\w/\U\0/g`
\$\endgroup\$
  • \$\begingroup\$ Use a heart to mark the unprintable \$\endgroup\$ – Leaky Nun Jun 20 '16 at 5:29
  • 2
    \$\begingroup\$ I thought this would be like vim but the ós are scaring me :| \$\endgroup\$ – Downgoat Jun 20 '16 at 5:39
2
\$\begingroup\$

s-lang, 7 bytes (Non-competing)

Try it here!

w[ ]Cc!

Explanation:

  • w[ ] splits the input string by spaces
  • C converts the selection (which is now the split-up words) to lower-case
  • c! c converts the selection to upper-case, but since it uses the parameter !, it only converts the first letter of each range of characters in the selection (which are words since we split by spaces)
\$\endgroup\$
  • 2
    \$\begingroup\$ The regex feature of this language is cool +1 \$\endgroup\$ – Downgoat Jun 20 '16 at 5:45
  • \$\begingroup\$ @Upgoat thanks! \$\endgroup\$ – MCMastery Jun 20 '16 at 5:55
2
\$\begingroup\$

Pyth, 6 bytes

jdm+rhd1rtd0c
jdrR4c

Test suite.

Split into words, capitalize each, then join by space.

It is not a built-in, because capitalize converts test case into Test case instead of Test Case.

(with built-ins)

r5

Test suite.

\$\endgroup\$
  • \$\begingroup\$ No built-ins are allowed so make the second one your top answer \$\endgroup\$ – Downgoat Jun 20 '16 at 5:36
  • 2
    \$\begingroup\$ @Upgoat See, this is why you don't change rules after answers have been posted. \$\endgroup\$ – Geobits Jun 20 '16 at 5:37
  • \$\begingroup\$ @Geobits idk it's just "no built-ins". It's not a radical rule change like other challenges have had \$\endgroup\$ – Downgoat Jun 20 '16 at 5:38
  • \$\begingroup\$ BTW, you can do 10 bytes even without r4. <space>uprHqGdQd \$\endgroup\$ – PurkkaKoodari Jun 20 '16 at 7:38
2
\$\begingroup\$

Ruby, 29 bytes

gets.gsub(/\S+/,&:capitalize)

I'm still learning ruby though >_>

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

Pyke, 5 bytes

cml5J

Try it here!

Split, map caps, join

\$\endgroup\$
  • \$\begingroup\$ Why does Pyke not default join by newlines :o \$\endgroup\$ – Leaky Nun Jun 20 '16 at 6:44
  • 1
    \$\begingroup\$ Because by default Pyke outputs the stack on multiple lines, 1 per element. It means I often don't need to join at all \$\endgroup\$ – Blue Jun 20 '16 at 7:35
0
\$\begingroup\$

Retina, 19 bytes

T`L`l
T`l`L`(^|\s).

Try it online!

\$\endgroup\$
  • \$\begingroup\$ T01`l`L`\S+ \$\endgroup\$ – Martin Ender Jun 20 '16 at 9:18
0
\$\begingroup\$

Python, 51 bytes

3 bytes thanks to xnor.

lambda n:' '.join(a.capitalize()for a in n.split())
lambda n:' '.join(map(str.capitalize,n.split()))

Ideone it!

using built-in, 28 bytes

from string import*
capwords

Ideone it!

\$\endgroup\$
  • \$\begingroup\$ I don't think this answer is valid without actually calling anything, or taking input. (unless I missed a meta post) \$\endgroup\$ – DJMcMayhem Jun 20 '16 at 5:30
  • \$\begingroup\$ It is a function, just like the ones in my challenge of binary logic gates \$\endgroup\$ – Leaky Nun Jun 20 '16 at 5:32
  • \$\begingroup\$ You can map the function str.capitalize in place of the list comp. Also, str.title seems to work. \$\endgroup\$ – xnor Jun 20 '16 at 6:06
  • \$\begingroup\$ @xnor str.title would convert Isn't into Isn'T. \$\endgroup\$ – Dennis Jun 20 '16 at 6:10
  • \$\begingroup\$ I didn't suggest map. xnor did. \$\endgroup\$ – Dennis Jun 20 '16 at 6:50

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