0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 3 years have passed since last update.

Josh Skeen, David Greenhalgh著 『Kotlinプログラミング』 章末問題に挑戦(第7章)

Last updated at Posted at 2020-12-13

 Josh Skeen, David Greenhalgh著『Kotlinプログラミング』には章末問題がある。残念なことに解答がないので、第4章に引き続き本を読み進めながら答案を記述する。
 もし回答に間違いや意見があれば、ぜひご指摘いただきたい。

第7章 章末問題 回答案

7.6 チャレンジ! DragonSpeakの改善

 toDragonSpeakを大文字に対応させる。
 本書p112のリスト7-5に示されたtoDragonSpeak()関数を次のリストのように変更する。
 正規表現Regex()関数が受け取る検索パターン引数を大文字にも対応するように"[aeiouAEIOU]"に変更し、when式の->演算子の右側にコンマで対応する大文字を追加した。

Tavern.kt
private fun toDragonSpeak(phrase: String) = phrase.replace(Regex("[aeiouAEIOU]")){
    when (it.value){
        "a","A" -> "4"
        "e","E" -> "3"
        "i","I" -> "2"
        "o","O" -> "1"
        "u","U" -> "|_|"
        else -> it.value
    }
}

第2章チャレンジ!答案
第3章チャレンジ!答案
第4章チャレンジ!答案
第7章チャレンジ!答案
第8章チャレンジ!答案

0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?