16 Jul 2013, 02:16
Dave_gnome_head_isolated_pragsmall

Dave Thomas (337 posts)

  • Write a function to capitalize the sentences in a string. Each sentence is terminated by a period and a space. Right now, the case of the characters in the string is random.

    iex> capitalize_sentences("oh. a DOG. woof. ")
    "Oh. A dog. Woof. "
    
A Possible Solution
defmodule MyString do

  def capitalize_sentences(string) do
    string
    |> String.split(%r{\.\s+})
    |> Enum.map(String.capitalize(&1))
    |> Enum.join(". ")
  end

end

IO.inspect MyString.capitalize_sentences("oh. a DOG. woof. ") 
07 Jan 2014, 00:59
Generic-user-small

Eric Liaw (3 posts)

irc chat discussion noted a fix for this should have a & in front of the String.capitalize, so it would instead be:

  def capitalize_sentences(string) do
    string
    |> String.split(%r{\.\s+})
    |> Enum.map(&String.capitalize(&1))
    |> Enum.join(". ")
  end
  You must be logged in to comment