altERnaTIng cAsE <=> ALTerNAtiNG CaSe

Elixir

defmodule Codewars.StringUtils do
  def alter_case(str) do
    to_string(
      Enum.map(
        String.graphemes(str),
        fn char -> change_char_case(char) end
      )
    )
  end

  def change_char_case(char) do
    if String.upcase(char) == char do
      String.downcase(char)
    else
      String.upcase(char)
    end
  end
end

Tags

  1. elixir (Private)
  2. 8-kyu (Private)
  3. codewars (Private)
  4. answer (Private)