34
\$\begingroup\$

This is a relatively quick one, but I'm sure you'll like it.

Codegolf a program that will take input in the form of a sentence and then provide the output with the first letter capitalized in each word.

Rules:

  1. Submissions may not be in the form of a function. So no:

    function x(y){z=some_kind_of_magic(y);return z;} as your final answer... Your code must show that it takes input, and provides output.

  2. The code must preserve any other capital letters the input has. So

    eCommerce and eBusiness are cool, don't you agree, Richard III?
    

    will be rendered as

    ECommerce And EBusiness Are Cool, Don't You Agree, Richard III?
    
  3. Some of you may be thinking, "Easy, I'll just use regex!" and so using the native regex in your chosen golfing language will incur a 30 character penalty which will be applied to your final code count. Evil laugh

  4. A "word" in this case is anything separated by a space. Therefore palate cleanser is two words, whereas pigeon-toed is considered one word. if_you_love_her_then_you_should_put_a_ring_on_it is considered one word. If a word starts with a non-alphabetical character, the word is preserved, so _this after rendering remains as _this. (Kudos to Martin Buttner for pointing this test case out).

    • 4b. There is no guarantee that words in the input phrase will be separated by a single space.
  5. Test Case, (please use to test your code):

    Input:

    eCommerce rocks. crazyCamelCase stuff. _those  pigeon-toed shennanigans. Fiery trailblazing 345 thirty-two Roger. The quick brown fox jumped over the lazy dogs. Clancy Brown would have been cool as Lex Luthor. good_bye
    

    Output:

    ECommerce Rocks. CrazyCamelCase Stuff. _those  Pigeon-toed Shennanigans. Fiery Trailblazing 345 Thirty-two Roger. The Quick Brown Fox Jumped Over The Lazy Dogs. Clancy Brown Would Have Been Cool As Lex Luthor. Good_bye
    
  6. This is code golf, shortest code wins...

Good luck...

\$\endgroup\$
  • 1
    \$\begingroup\$ What about spaces at the end of the line? Do we have to preserve them? Can we add one if it serves our needs? \$\endgroup\$ – Dennis May 11 '15 at 0:48
  • 2
    \$\begingroup\$ Dennis, please preserve spaces from the input... \$\endgroup\$ – WallyWest May 11 '15 at 1:04
  • 3
    \$\begingroup\$ != TitleCase dam it! c# loses AGAIN! \$\endgroup\$ – Ewan May 11 '15 at 11:07
  • 1
    \$\begingroup\$ @Tim The double space before Pigeon-toed is correct. He said to preserve spacing. \$\endgroup\$ – mbomb007 May 11 '15 at 17:04
  • 2
    \$\begingroup\$ What separates the words? Any whitespace (tabs, newlines, etc) or just spaces? \$\endgroup\$ – Steven Rumbalski May 11 '15 at 18:22

56 Answers 56

21
\$\begingroup\$

CJam, 15 13 bytes

Lq{_eu?_S-}/;

Try it online in the CJam interpreter.

Pseudocode

L             e# B := ""
 q            e# Q := input()
  {       }/  e# for C in Q:
   _eu?       e#     C := B ? C : uppercase(C)
       _S-    e#     B := string(C).strip(" ")
            ; e# discard(B)

All modified characters C are left on the stack and, therefore, printed when exiting.

\$\endgroup\$
  • 3
    \$\begingroup\$ Damn this is clever. D: \$\endgroup\$ – Martin Ender May 11 '15 at 0:25
  • \$\begingroup\$ I have to agree, outgolfing someone by 4 chars in a codegolf language is a feat in itself... well done. \$\endgroup\$ – WallyWest May 11 '15 at 1:07
  • 12
    \$\begingroup\$ @WallyWest: Golfing languages can give the impression that they kinda golf themselves, but I assure you that they don't. TMTOWTDI is true for all languages and especially for those with a lot of built-ins. Sometimes you win, sometimes you lose and sometimes you feel like you've been hit by a truck. \$\endgroup\$ – Dennis May 11 '15 at 1:30
13
\$\begingroup\$

CSS 2.1, 49

:after{content:attr(t);text-transform:capitalize}

Explanation:

  • The attr function takes the input from a t (text) HTML attribute.
  • The input is capitalized by setting text-transform to capitalize.
  • The output is provided as a generated content, using the content property on an ::after pseudo-element.

Runnable snippet:

:after {
    content: attr(t);
    text-transform: capitalize;
}
<div t="eCommerce rocks. crazyCamelCase stuff. _those  pigeon-toed shennanigans. Fiery trailblazing 345 thirty-two Roger. The quick brown fox jumped over the lazy dogs. Clancy Brown would have been cool as Lex Luthor. good_bye"></div>

Note: CSS 2.1 specified the desired behavior: capitalize uppercased the first character of each word. However, CSS3 uppercases first typographic letter unit of each word. So the snippet above won't work properly neither on old IE, which didn't follow CSS 2.1; nor on new compliant browsers which follow CSS3.

\$\endgroup\$
  • \$\begingroup\$ Oh, this is clever! \$\endgroup\$ – IQAndreas May 12 '15 at 6:28
  • 1
    \$\begingroup\$ (too bad about the _those problem on CSS3 browsers, but I'm still upvoting because of the unique way of solving the problem.) \$\endgroup\$ – IQAndreas May 12 '15 at 6:29
  • \$\begingroup\$ @Oriol, "oh this is clever!" indeed! Sorry IQAndreas, I have to borrow your comment here... this is an ingenious approach to solving the problem... i will have to make use of this approach... \$\endgroup\$ – WallyWest May 21 '15 at 11:16
10
\$\begingroup\$

Javascript (ES6), 77 bytes

alert(prompt().split(' ').map(x=>x&&x[0].toUpperCase()+x.slice(1)).join(' '))

Commented

alert( // output
    prompt(). // take input
    split(' '). // split by spaces
    map(x=> // map function to array
        x && // if x, empty string "" is falsey and returns itself
        x[0].toUpperCase() + x.slice(1) // capaitalize 1st char and concatenate the rest
    ).
    join(' ') // join array with spaces
)
\$\endgroup\$
  • \$\begingroup\$ What happens if words are separated by multiple spaces? [4b] \$\endgroup\$ – Caek May 11 '15 at 2:35
  • 3
    \$\begingroup\$ @Caek It's handled by the x&&. An empty string is falsey so the && short-circuits and returns the left operand, the empty string. Spaces are preserved. \$\endgroup\$ – nderscore May 11 '15 at 2:38
  • \$\begingroup\$ Awesome, thanks for the explanation. Might help me figure out how I can get it to work now. \$\endgroup\$ – Caek May 11 '15 at 2:49
  • \$\begingroup\$ This will capitalize even non Ascii characters, so that å will become Å! \$\endgroup\$ – leo May 13 '15 at 8:38
9
\$\begingroup\$

Perl, 13 bytes

perl -040pe '$_="\u$_"'

9 bytes plus 4 bytes for 040p (assuming I've interpreted the rules on special invocations correctly).

-040 sets the input record separator $/ to a single space, so spaces are preserved; the \u escape sequence converts the next character to title case.

\$\endgroup\$
  • \$\begingroup\$ Great work, honorable mention for using the command line! \$\endgroup\$ – WallyWest May 27 '15 at 22:28
7
\$\begingroup\$

CJam, 17 15 bytes

lS/{S+(eu\+}/W<

Test it here.

Fairly straightforward implementation of the spec. Make use of the new {}& to avoid errors for consecutive spaces.

Two bytes saved by Dennis.

\$\endgroup\$
  • \$\begingroup\$ Great stuff! Is CJam primarily just a golfing language or does it have some practical commercial applications? \$\endgroup\$ – WallyWest May 10 '15 at 23:47
  • 6
    \$\begingroup\$ @WallyWest No it's just a golfing language. It definitely doesn't have commercial applications, but I personally use it occasionally for quick throw-away scripts (because it has a lot of built-ins, and if you know what you're doing, then typing fewer characters is quicker than typing more characters ;)). \$\endgroup\$ – Martin Ender May 10 '15 at 23:49
  • \$\begingroup\$ You can save a few bytes by appending a space to each word. Depending on the OP's answer to my question, this could get you to either 14 or 12 bytes. \$\endgroup\$ – Dennis May 11 '15 at 0:58
  • \$\begingroup\$ @Dennis Oh right, I was playing around with that, but didn't consider simply adding it before pulling off the first character. I'll change that tomorrow, thank you! \$\endgroup\$ – Martin Ender May 11 '15 at 1:00
  • \$\begingroup\$ @Dennis Thanks, I changed it, but I'm not sure what 14-byte version you meant. If you're talking about omitting the second +, then that breaks if the input contains trailing spaces. \$\endgroup\$ – Martin Ender May 11 '15 at 9:02
7
\$\begingroup\$

C, 64 63 bytes

a;main(c){while(~(c=getchar()))putchar(a?c:toupper(c)),a=c-32;}

Fix: some compilers (such as Clang) don't like an int parameters in place of argv, so I moved it to a global variable. The byte count stays the same. Thanks to squeamish ossifrage for noticing. Down to 63 bytes, thanks Dennis.

Ungolfed:

int a;

int main(int c) {
    while(~(c = getchar()))
        putchar(a ? c : toupper(c)),
        a = c - ' ';
}

Pretty straightforward: if a is false, the character is converted to uppercase. It is set after reading a space: c - ' ' is false only if c == ' '. toupper() ignores everything that is not a lowercase letter, so symbols and multiple spaces are fine. -1 has all bits set, so when getchar() returns -1 the NOT operator makes it zero, and the loop stops. a is declared as a global variable, so it is initializd to zero (false). This ensures that the first word is capitalized.

\$\endgroup\$
  • 1
    \$\begingroup\$ while(~(c=getchar()) — I like that. Clang won't actually compile this, but you can get the same character count with c;main(a){...} \$\endgroup\$ – squeamish ossifrage May 11 '15 at 13:24
  • 1
    \$\begingroup\$ If you swap the declarations of a and c and the order of the ternary operator, you can replace == with - to save one byte. \$\endgroup\$ – Dennis May 11 '15 at 16:45
  • \$\begingroup\$ You are right, of course. \$\endgroup\$ – Andrea Biondo May 11 '15 at 17:10
  • \$\begingroup\$ Nice! +1 The program would work the same when using while(!(c = getchar())), right? \$\endgroup\$ – Spikatrix May 13 '15 at 11:17
  • 1
    \$\begingroup\$ @Cool Guy: Nope, the bitwise ~ and the logical ! are not the same. In C anything that is not zero is considered true, so your condition would be like while((c = getchar()) == 0) which of course won't work. The bitwise NOT operator ~ negates the value bit-by-bit. To break the loop, ~c must be zero: this means that all bits have to be one, so that when negated they become all zeroes. That value (for a 32bit int) is 0xFFFFFFFF, which, if signed, is -1 (EOF). \$\endgroup\$ – Andrea Biondo May 13 '15 at 12:38
7
\$\begingroup\$

Python 3, 59 56 bytes

f=1
for c in input():print(end=f*c.upper()or c);f=c==" "

Thanks to @Reticality for 3 bytes.

\$\endgroup\$
  • 3
    \$\begingroup\$ How about print(end=f*c.upper()or c)? That would save 4 bytes \$\endgroup\$ – user34736 May 11 '15 at 15:45
  • \$\begingroup\$ @Reticality Oh wow, I had no idea you could have an empty print with just a keyword arg. Thanks! \$\endgroup\$ – Sp3000 May 11 '15 at 23:01
7
\$\begingroup\$

Perl Version < 5.18, 30 27 26 25

say map"\u$_",split$,=$"

24 characters +1 for -n.

\u makes the next character in a string uppercase. @ThisSuitIsBlackNot pointed this out to save 1 byte. Before we were using the function ucfirst.

From the perldocs,

As another special case, split emulates the default behavior of the command line tool awk when the PATTERN is either omitted or a literal string composed of a single space character (such as ' ' or "\x20" , but not e.g. / / ). In this case, any leading whitespace in EXPR is removed before splitting occurs, and the PATTERN is instead treated as if it were /\s+/ ; in particular, this means that any contiguous whitespace (not just a single space character) is used as a separator. However, this special treatment can be avoided by specifying the pattern / / instead of the string " " , thereby allowing only a single space character to be a separator. In earlier Perls this special case was restricted to the use of a plain " " as the pattern argument to split, in Perl 5.18.0 and later this special case is triggered by any expression which evaluates as the simple string " " .

Since $" evaluates to a space, this will preserve the spaces. Since we want to both set $, to a space character, and input a space character to the split, @nutki pointed out we can do both as the input to the split. That saves 3 bytes from what we had before, which was first setting $, and then inputting $" to the split.

Using a , for map instead of {} saves an additional byte, as @alexander-brett pointed out.

Run with:

echo 'eCommerce     rocks. crazyCamelCase stuff. _those  pigeon-toed shennanigans. Fiery trailblazing 345 thirty-two Roger. The quick brown fox jumped over the lazy dogs. Clancy Brown would have been cool as Lex Luthor. good_bye' | perl -nE'say map"\u$_",split$,=$"'
\$\endgroup\$
  • 1
    \$\begingroup\$ Save 1 byte with ...map"\u$_",split... \$\endgroup\$ – alexander-brett May 12 '15 at 15:19
  • \$\begingroup\$ @alexander-brett thanks! I updated the answer. \$\endgroup\$ – chilemagic May 12 '15 at 16:51
5
\$\begingroup\$

><> (Fish), 39 bytes

</?-' 'o:;?(0:<-*' '*('{'$)'`'::i
i/.0e

Method:

  • Take one char and capitalize it if in range a-z then print it out. (left-to-right code for this part is i::'backquote')$'{'(*' '*+)
  • If the last taken char is an EOF char then exit else print it
  • If the last taken char is a space char then go to point 1 else take a new letter and go to point 2.
\$\endgroup\$
5
\$\begingroup\$

JAVA, 273 bytes

EDIT

import static java.lang.System.*;class x{public static void main(String[] s){char[] a=new java.util.Scanner(in).nextLine().toCharArray();boolean f=1>0;for(int i=0;i<a.length;i++){if(a[i]==' '){f=1>0;continue;}if(f){a[i]=Character.toUpperCase(a[i]);f=1<0;}}out.println(a);}}
\$\endgroup\$
  • \$\begingroup\$ This is my first answer in PCG, not sure if this is acceptable. \$\endgroup\$ – Atul Kumbhar May 11 '15 at 2:15
  • \$\begingroup\$ Welcome aboard! You might try removing whitespace and using single characters for variable names. There are some other tips for golfing JAVA as well. \$\endgroup\$ – nderscore May 11 '15 at 2:19
  • \$\begingroup\$ Thanks @nderscore for the hint, I have edited my answer using the tips. \$\endgroup\$ – Atul Kumbhar May 11 '15 at 2:51
  • \$\begingroup\$ Looking better! I also added the byte count into your post for you. \$\endgroup\$ – nderscore May 11 '15 at 3:26
  • 1
    \$\begingroup\$ @TuukkaX He doesn't have public in front of the class.. And if you mean he can remove the public in front of the static void main(..., then you are wrong, unless he also changes the class to interface and uses Java 8+. \$\endgroup\$ – Kevin Cruijssen Jun 20 '16 at 13:01
5
\$\begingroup\$

JavaScript (regex solution) - 104 bytes

Someone has to bite the bullet and post the RegEx solution! 74 characters, plus the +30 character penalty:

alert(prompt().replace(/(^| )[a-z]/g,function(m){return m.toUpperCase()}))

Or if you want to read and understand the code in its non-compacted fashion:

//     Matches the (beginning of the line or a space), followed by a lowercase English character.  
string.replace( /(^| )[a-z]/g ,
                function(match) { return match.toUpperCase(); }
\$\endgroup\$
  • 1
    \$\begingroup\$ Clever... though of course, you've paid the price with a 30 character penalty... I take my hat off to you for biting the bullet... \$\endgroup\$ – WallyWest May 21 '15 at 11:12
4
\$\begingroup\$

Python 2, 73 bytes

i=raw_input()
print''.join((c,c.upper())[p==' ']for p,c in zip(' '+i,i))

This program capitalises a letter if preceded by a space (with a kludge for the first character in the string). It relies on the .upper() string method to capitalise correctly.

\$\endgroup\$
  • 2
    \$\begingroup\$ You could save 2 bytes by porting to Python 3. (-4 raw_input => input, +2 print => print()) \$\endgroup\$ – Steven Rumbalski May 11 '15 at 19:27
  • \$\begingroup\$ Thanks Steven. I had considered the savings in bytes by coding in Python 3. Then I thought, if I was to change language to be competitive, I would change to Pyth. I am happy to compete in the Python 2 sub-league. I code in Python 2 every day for work, so this experience makes me better at my job (but my work code is not golfed!). \$\endgroup\$ – Logic Knight May 16 '15 at 11:11
4
\$\begingroup\$

PHP 64 76 77 83 84 89 bytes

Does $_GET count as input in PHP?
If so, here is my first CG attempt

foreach(explode(' ',$_GET[@s])as$k=>$v)echo$k?' ':'',ucfirst($v)

Thanks manatwork :)

One could just use the ucwords function, which would result in 21 bytes:

<?=ucwords($_GET[@s])

thanks Harry Mustoe-Playfair :)

\$\endgroup\$
  • \$\begingroup\$ Personally I consider only fgets(STDIN) to read input. But we have no consensus on $_GET as far as I know. \$\endgroup\$ – manatwork May 11 '15 at 9:52
  • \$\begingroup\$ Yup, that works :D \$\endgroup\$ – fox May 11 '15 at 10:16
  • \$\begingroup\$ You don't need the tricks to shut up the warnings. Thei're warnings! Nobody cares about them. \$\endgroup\$ – Ismael Miguel May 11 '15 at 13:05
  • \$\begingroup\$ Well, didn't thought of that. Guess I'll have to stick to substr \$\endgroup\$ – fox May 11 '15 at 14:05
  • \$\begingroup\$ No need for that. It's just time to forget my earlier advice on removing $k=>. Put it back: foreach(split(' ',$_GET[@s])as$k=>$v)echo$k?' ':'',ucfirst($v); \$\endgroup\$ – manatwork May 11 '15 at 14:39
4
\$\begingroup\$

Haskell, 69

import Data.Char
main=interact$tail.scanl(!)' '
' '!c=toUpper c;_!c=c

Explanation:

scanl takes a function (a -> b -> a) and an initial value a, then iterates over a list of [b]s to make a list of [a]s:

scanl (!) z [a,b,c] == [   z
                       ,   z ! a
                       ,  (z ! a) ! b
                       , ((z ! a) ! b) ! c]

It repeatedly takes the previous result as the left argument of the function passed to it, and a value from the input list as the right argument, to make the next one.

I wrote a function (!) :: Char -> Char -> Char that returns the right character you pass it, but capitalizes it if the left char is ' ' (space). For scanl, this means: return the value from the input list, but capitalize it if the previous result was a space. So scanl (!) ' ' "ab cd" becomes:

    scanl (!) ' ' "ab cd"
==> ' ' : scanl (!) (' ' ! 'a') "b cd"
==> ' ' : scanl (!)     'A'     "b cd"
==> ' ' : 'A' : scanl (!) ('A' ! 'b') " cd"
==> ' ' : 'A' : scanl (!)     'b'     " cd"
==> ' ' : 'A' : 'b' : scanl (!) ('b' ! ' ') "cd"
==> ' ' : 'A' : 'b' : scanl (!)     ' '     "cd"
==> ' ' : 'A' : 'b' : ' ' : scanl (!) (' ' ! 'c') "d"
==> ' ' : 'A' : 'b' : ' ' : scanl (!)     'C'     "d"
==> ' ' : 'A' : 'b' : ' ' : 'C' : scanl (!) ('C' ! 'd') ""
==> ' ' : 'A' : 'b' : ' ' : 'C' : scanl (!)     'd'     ""
==> ' ' : 'A' : 'b' : ' ' : 'C' : 'd' : ""
==> " Ab Cd"

We need the initial value ' ' to capitalize the first letter, but then we chop it off with tail to get our final result.

\$\endgroup\$
  • \$\begingroup\$ Nice! Can you please explain it for me? \$\endgroup\$ – poida May 12 '15 at 10:23
  • \$\begingroup\$ I wrote an explanation. \$\endgroup\$ – Lynn May 12 '15 at 15:06
  • \$\begingroup\$ Some more scanl examples: one, two. \$\endgroup\$ – Lynn May 12 '15 at 15:14
  • \$\begingroup\$ @Mauris kudos for using such a great algorithmm like this... :) \$\endgroup\$ – WallyWest May 21 '15 at 11:11
3
\$\begingroup\$

Pyth, 20 bytes

uXGHr@GH1fqd@+dzTUzz

These multiple spaces really sucks. Otherwise there would have been a really easy 12 bytes solution.

Try it online: Pyth Compiler/Executor

Explanation

                      implicit: z = input string
         f       Uz   filter [0, 1, 2, ..., len(z)-1] for elements T, which satisfy:
          qd@+dzT        " " == (" " + z)[T]
                      (this finds all indices, which should be capitalized)
u                  z  reduce, start with G = z, for H in idices ^ update G by
 XGH                     replace the Hth char of G by
    r   1                upper-case of
     @GH                 G[H]
                      implicitly print result

edit: 16 chars is possible with @Dennis algorithm.

\$\endgroup\$
  • 1
    \$\begingroup\$ The multiple space thing is there to make it a lot more challenging... otherwise it would be a simple case of string.split(" ") or something similar... But you've done well to do it in 20 characters \$\endgroup\$ – WallyWest May 11 '15 at 1:05
3
\$\begingroup\$

CJam, 14 bytes

It's not the shortest, but...

qS/Sf.{\eu}s1>

Another answer using similar ideas:

qS/Laf.{;eu}S*

.x only changes the first item if one of the parameters has only one item.

\$\endgroup\$
  • 1
    \$\begingroup\$ Chaining f and . is pretty ingenious. Another 14 bytes variant: qS/Sf.{\eu}S.- \$\endgroup\$ – Dennis May 12 '15 at 15:44
3
\$\begingroup\$

Lua, 64 62 61 bytes

Lua is a horrendous language to golf in, so I'm pretty proud of myself for this one.

print(string.gsub(" "..io.read(),"%s%l",string.upper):sub(2))

[Try it here]1 Outdated, will update tommorow

\$\endgroup\$
  • 1
    \$\begingroup\$ Welcome to PPCG! Surely, you don't need those spaces after commas? \$\endgroup\$ – Martin Ender May 11 '15 at 23:59
  • \$\begingroup\$ Wow, I'm so new to this I didnt even know spaces counted. 62 bytes! \$\endgroup\$ – user40734 May 12 '15 at 0:52
  • 2
    \$\begingroup\$ I also just noticed it's not entirely correct: you are capitalising letters after all non-letters, so abc_def will give Abc_Def. However only letters after spaces should be turning into upper case. The good news is, fixing it saves a byte. ;) \$\endgroup\$ – Martin Ender May 12 '15 at 0:57
3
\$\begingroup\$

JAVA, 204 211 226 bytes

My first entry on CG, I hope it's fine:

class U{public static void main(String[]s){int i=0;char[]r=s[0].toCharArray();r[0]=Character.toUpperCase(r[0]);for(char c:r){if(c==' '&&i>0)r[i+1]=Character.toUpperCase(r[i+1]);i++;System.out.print(c);}}}

Saved 7 bytes thanks to @TNT

\$\endgroup\$
  • \$\begingroup\$ Involving my poor Java skills: public class U{public static void main(String[]s){int i=-1,j;char[]r=s[0].toCharArray();for(char c:r)if(++i==0||c==' '&&i>0)r[j=i+(i==0?0:1)]=Character.toUpperCase(r[j]);System.out.print(r);}} \$\endgroup\$ – manatwork May 11 '15 at 16:36
  • 1
    \$\begingroup\$ Welcome to PPCG! The public modifier isn't necessary so you can save 7 more. \$\endgroup\$ – TNT May 11 '15 at 16:38
3
\$\begingroup\$

PHP: 76 74 characters

foreach($l=str_split(fgets(STDIN))as$c){echo$l?ucfirst($c):$c;$l=$c==" ";}

Sample run:

bash-4.3$ php -r 'foreach($l=str_split(fgets(STDIN))as$c){echo$l?ucfirst($c):$c;$l=$c==" ";}' <<< 'eCommerce rocks. crazyCamelCase stuff. _those  pigeon-toed shennanigans. Fiery trailblazing 345 thirty-two Roger. The quick brown fox jumped over the lazy dogs. Clancy Brown would have been cool as Lex Luthor. good_bye'
ECommerce Rocks. CrazyCamelCase Stuff. _those  Pigeon-toed Shennanigans. Fiery Trailblazing 345 Thirty-two Roger. The Quick Brown Fox Jumped Over The Lazy Dogs. Clancy Brown Would Have Been Cool As Lex Luthor. Good_bye
\$\endgroup\$
  • \$\begingroup\$ Instead of ucfirst($c), use $c^' '. (Tip: if you bitwise-xor a letter with a space, it will be converted from uppercase to lowercase, and the oposite applies too) \$\endgroup\$ – Ismael Miguel May 12 '15 at 15:50
  • \$\begingroup\$ @IsmaelMiguel, that works fine in your solution as you process only lowercase letters. But in my solution all first characters are processed. So for the (otherwise great) xor trick my code would also need some character type filtering. :( \$\endgroup\$ – manatwork May 12 '15 at 15:59
  • \$\begingroup\$ That didn't crossed my mind. There must be a bitwise trick to check if it is a letter or not. \$\endgroup\$ – Ismael Miguel May 12 '15 at 16:08
  • 1
    \$\begingroup\$ One thing you can do is $l=str_split(fgets(STDIN)), which reduces the code by 2 bytes! \$\endgroup\$ – Ismael Miguel May 12 '15 at 17:10
  • 1
    \$\begingroup\$ Now I'm going mad. Man, how long I starred to that initialization and missed it. Thank you, @IsmaelMiguel. \$\endgroup\$ – manatwork May 12 '15 at 17:14
3
\$\begingroup\$

C, 74 bytes

a,b=1;main(){while((a=getchar())>0)b=isspace(putchar(b?toupper(a):a));}

Makes no assumptions about the run-time character set (ASCII, EBCDIC, Baudot, ...whatever). Does assume that EOF is negative (I think C guarantees that).

a,b=1;
main()
{
    while((a=getchar())>0)
        b=isspace(putchar(b?toupper(a):a));
}

a is the input character; b is true if the last character was space. The only non-obvious bit is that we use the fact that putchar returns the character printed if there's no error.

\$\endgroup\$
3
\$\begingroup\$

C# Linq - 187

This is nowhere close to winning but I just love Linq too much.

namespace System{using Linq;class P{static void Main(string[]a){Console.Write(a[0].Substring(1).Aggregate(a[0][0].ToString().ToUpper(),(b,c)=>b[b.Length-1]==32?b+char.ToUpper(c):b+c));}}}
\$\endgroup\$
3
\$\begingroup\$

Vim, 11, 10 bytes

qqvUW@qq@q

Explanation:

qq           #Start recording in register 'q'
  vU        #Make the character under the cursor uppercase
     W       #Move forward a WORD
      @q     #recursively call macro 'q'
        q    #stop recording
         @q  #Call the recursive macro

Do I get a gold-badge for outgolfing Dennis?

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

Bash, 61

a="${@//: / }"
a=(${a//: / })
a="${a[@]^}"
echo "${a//:/ }"

Note the colons are simply to make the program display OK here. In reality these can be some non-printable character, such as BEL.

Output

$ ./cap1st.sh "eCommerce rocks. crazyCamelCase stuff. _those  pigeon-toed shennanigans. Fiery trailblazing 345 thirty-two Roger. The quick brown fox jumped over the lazy dogs. Clancy Brown would have been cool as Lex Luthor. good_bye"
ECommerce Rocks. CrazyCamelCase Stuff. _those  Pigeon-toed Shennanigans. Fiery Trailblazing 345 Thirty-two Roger. The Quick Brown Fox Jumped Over The Lazy Dogs. Clancy Brown Would Have Been Cool As Lex Luthor. Good_bye
$ 

Bash, 12

Sadly this one doesn't preserve leading/mutliple/trailing spaces, but otherwise it works:

echo "${@^}"

Output

$ ./cap1st.sh eCommerce rocks. crazyCamelCase stuff. _those  pigeon-toed shennanigans. Fiery trailblazing 345 thirty-two Roger. The quick brown fox jumped over the lazy dogs. Clancy Brown would have been cool as Lex Luthor. good_bye
ECommerce Rocks. CrazyCamelCase Stuff. _those Pigeon-toed Shennanigans. Fiery Trailblazing 345 Thirty-two Roger. The Quick Brown Fox Jumped Over The Lazy Dogs. Clancy Brown Would Have Been Cool As Lex Luthor. Good_bye
$ 
\$\endgroup\$
  • 5
    \$\begingroup\$ But that's half the challenge! \$\endgroup\$ – Sp3000 May 11 '15 at 2:47
  • 1
    \$\begingroup\$ @Sp3000 there I fixed it (at as cost of 49 chars) \$\endgroup\$ – Digital Trauma May 11 '15 at 3:41
2
\$\begingroup\$

Pip, 15 + 1 for -s = 16

{IaUC:a@0a}Ma^s

Explanation:

                  a is first cmdline arg (implicit)
            a^s   Split a on spaces
{         }M      Map this function to each element:
 Ia                 If the word is not empty,
   UC:a@0             uppercase its first character
         a          Return the word
                  Output the resulting list (implicit) joined on spaces (-s flag)

One interesting feature of Pip that this program draws on is the : assignment meta-operator. Most C-like languages have some set of compute-and-assign operators: e.g. x*=5 does the same thing as x=x*5. In Pip, however, you can tack : onto any operator and turn it into a compute-and-assign operator. This even goes for unary operators. So -:x computes -x and assigns it back to x, the same as x:-x would. In this case, UC: is used (together with Pip's mutable strings) to uppercase the first character of a word.

The program takes input from the command-line, requiring an invocation like this:

python3 pip.py -se "{IaUC:a@0a}Ma^s" "test teSt TEST  _test"
\$\endgroup\$
2
\$\begingroup\$

C, 125

Not the shortest of solutions, but I really like to golf in C.

char b[99];main(c){while(scanf("%[A-Za-z_-]",b)==1)islower(*b)&&(*b&=223),printf("%s",b);~(c=getchar())&&putchar(c)&&main();}

ungolfed:

char b[99];
main(c)
{
  while(scanf("%[A-Za-z_-]", b) == 1) {
    if(islower(b[0])) {
      b[0] &= 0xDF;
    }
    printf("%s", b);
  }
  if((c = getchar()) != -1) {
      putchar(c);
      main();
  }
}

I don't know wheter using regex-like syntax in scanf is streching the rules, but it works quite nicely. (Well, technically it's not a full regex)

An other thing to consider is that this code only works for words shorter than 99 bytes. But I think this solution will work for most cases.

\$\endgroup\$
  • \$\begingroup\$ Hint: &=223 --> -=32 \$\endgroup\$ – edc65 May 11 '15 at 8:19
2
\$\begingroup\$

Haskell: 127 characters

import Data.List
import Data.Char
i=isSpace
s a b=i a==i b
u (w:ws)=(toUpper w):ws
f w=concatMap u$groupBy s w
main=interact f
\$\endgroup\$
  • \$\begingroup\$ I got down to 69 bytes. \$\endgroup\$ – Lynn May 11 '15 at 17:11
2
\$\begingroup\$

PHP, 82

echo join(' ',array_map(function($s){return ucfirst($s);},explode(' ',$argv[1])));

Usage :

$ php code.php "eCommerce rocks. crazyCamelCase stuff. _those  pigeon-toed shennanigans. Fiery trailblazing 345 thirty-two Roger. The quick brown fox jumped over the lazy dogs. Clancy Brown would have been cool as Lex Luthor. good_bye"
\$\endgroup\$
2
\$\begingroup\$

C#, 133 131

using C=System.Console;class P{static void Main(){var s=2>1;foreach(var c in C.ReadLine()){C.Write(s?char.ToUpper(c):c);s=c==32;}}}
\$\endgroup\$
  • \$\begingroup\$ Do you need &&c!=32? I'm not too fluent in C#, but I would guess that converting a space to uppercase results in a space. \$\endgroup\$ – DLosc May 11 '15 at 4:01
  • \$\begingroup\$ Whoops, thanks - that was from before I made some other changes, I think. You're correct it's not needed. \$\endgroup\$ – Blorgbeard May 11 '15 at 4:25
  • \$\begingroup\$ try "using C=System.Console;" instead of using system \$\endgroup\$ – Ewan May 11 '15 at 11:38
2
\$\begingroup\$

Mathematica, 66 bytes

Print@StringReplace[InputString[],WordBoundary~~a_:>ToUpperCase@a]

I would use ToCamelCase, but it doesn't preserve spacing.

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

R, 139 105 bytes

for(i in 1:length(p<-strsplit(readline(),"")[[1]])){if(i<2||p[i-1]==" ")p[i]=toupper(p[i])};cat(p,sep="")

Ungolfed + explanation:

# Assign p to be a vector of the input read from stdin, split into characters

for(i in 1:length(p <- strsplit(readline(), "")[[1]])) {

    # If we're at the first iteration or the previous character was a space

    if (i < 2 || p[i-1] == " ") {

        # Convert the current character to its uppercase equivalent

        p[i] <- toupper(p[i])
    }
}

# Join the vector elements into a single string and print it to stdout
cat(p, sep = "")

R with regex, 49 41 + 30 = 71 bytes

I'm really bummed; this actually has a better score using regular expressions with the penalty.

gsub("(^.| +.)","\\U\\1",readline(),pe=T)

This matches any single character at the beginning of the string or following any number of spaces and replaces it with an uppercase version of the capture. Note that applying \\U is legit and has no effect for non-letters. pe=T is interpreted as perl = TRUE since it takes advantage of R's partial matching of function parameters and the synonym for TRUE. For whatever reason, R doesn't use Perl-style regular expression by default.

Thanks to MickyT for helping save 8 bytes on the regex approach!

\$\endgroup\$
  • \$\begingroup\$ With your regex the matching string could be (^.| +.). Uppercasing anything is OK. \$\endgroup\$ – MickyT May 11 '15 at 20:49
  • \$\begingroup\$ @MickyT: Good idea, thanks! Edited to use your suggestion. \$\endgroup\$ – Alex A. May 11 '15 at 20:54

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.