C#3 Let’s capitalize on it

Here is a new challenge for you – capitalize the first letter of each word in a string.

For example:

q) f "I’m gonna make him an offer he can’t refuse"
"I'm Gonna Make Him An Offer He Can't Refuse"

Post your answers in the comment section below and let me know if you have any questions.

P.S. Don’t worry about the case with a trailing space i.e. “Hi how are you “

UPDATE:

Pet usa discount cialis birds can be quite high-priced. A decreased estimations of tadalafil may be the suitable and favorable option for the male suffering from impotence problem. free cialis no prescription Remember, fault is not in suffering, it’s in hiding. cute-n-tiny.com vardenafil price Certain fruits have been recognized to have much higher nutritional substance and antioxidant properties canadian levitra online than others.

Great solutions everyone! We have quite a variety this time.

Here are all the solutions:

David - {@[x;0,1+where x=" ";upper]} (a more complete version in comments)
Rolf - {@[x;where" "=" ",x;upper]}
Showvik - {ssr[c;” ?”;upper]}
Bill - {s:” ” vs trim x;” “sv .[s;(til count s;0);upper]}
Me - {" " sv .[b;((til count b:" " vs x);(0;0));upper]}

David and Rolf have similar solutions. They used @ to modify the string.
Showvik has a more simplistic approach. He used ssr function to search and replace parts of the string. The only issue is that it won’t cover the first word but that wasn’t specified as a requirement.

Bill and my solutions are both very similar (pretty much the same). We break down the string first and then modify at depth using . operator and then reconstruct the string. It’s a little sophisticated and it’s simpler to use @ approach.

Thanks everyone for participating!

Join the Conversation

7 Comments

  1. how about:
    {@[x;0,(-1+count x)&1+where x=” “;upper]}

    or if there is a trailing space:
    {@[x;0,(-1+count x)&1+where x=” “;upper]}

  2. q)c
    “I’m gonna make him an offer he can’t refuse”
    q)ssr[c;” ?”;upper]
    “I’m Gonna Make Him An Offer He Can’t Refuse”
    q)

      1. Yea but I guess it’s my fault for not mentioning that in the post and using an example that had first letter capitalized. 🙁

  3. Solution that handles trailing/leading whitespace:
    f:{s:” ” vs trim x;” “sv .[s;(til count s;0);upper]}

  4. My solution is messier, but surprisingly it performs a bit faster and uses the least memory.
    {sm:” ” vs x; sm[;0]:.Q.A[.Q.a?lower sm[;0]]; ” ” sv sm}

    Performance on 10,000 iterations:
    x:”I’m gonna make him an offer he can’t refuse”;

    // david
    {@[x;0,1+where x=” “;upper]}x
    \ts do[10000;{@[x;0,1+where x=” “;upper]}x] //254 3792j
    // rolf
    {@[x;where” “=” “,x;upper]}x
    \ts do[10000;{@[x;where” “=” “,x;upper]}x] //253 3792j
    // showvik
    {ssr[x;” ?”;upper]}x
    \ts do[10000;{ssr[x;” ?”;upper]}x] //278 4800j
    // bill
    {s:” ” vs trim x;” “sv .[s;(til count s;0);upper]}x
    \ts do[10000;{s:” ” vs trim x;” “sv .[s;(til count s;0);upper]}x] //306 4416j
    // ENLIST Q guy
    {” ” sv .[b;((til count b:” ” vs x);(0;0));upper]}x
    \ts do[10000;{” ” sv .[b;((til count b:” ” vs x);(0;0));upper]}x] //551 4512j
    // patrick
    {sm:” ” vs x; sm[;0]:.Q.A[.Q.a?lower sm[;0]]; ” ” sv sm}s
    \ts do[10000;{sm:” ” vs x; sm[;0]:.Q.A[.Q.a?lower sm[;0]]; ” ” sv sm}x] //60 3712j

  5. you can modify stuff in-place.

    s:”I’m gonna make him an offer he can’t refuse”;
    l:”i”$trim lower s; / normalize
    l[0,1+where l=” “]-:32;”c”$l

Leave a comment

Your email address will not be published. Required fields are marked *