// TODO 1/5: does a supplied string have a vowel? fun hasAVowel(s: String): Boolean { for (c in s) { if (c.lowercase() in "aeiouy") { return true } } return false } println(hasAVowel("Howdy")) // yes! println(hasAVowel("Act")) // yes! println(hasAVowel("Rrrgggh")) // no! // TODO 2/5: index of the first vowel, or -1 fun firstVowel(s: String): Int { for (idx in s.indices) { if (s[idx].lowercase() in "aeiouy") { return idx } } return -1 } println(firstVowel("Howdy")) // 1 println(firstVowel("Act")) // 0 println(firstVowel("Rrrgggh")) // -1 // TODO 3/5: print a string in reverse // (with a new line AFTER) fun printReverse(s: String) { for (idx in s.indices.reversed()) { print(s[idx]) } println() } printReverse("howdy") // should be ydwoh on one line // TODO 4/5: print a rectangle of @'s // (they're listening to you...) fun drawBox(rows: Int, cols: Int, c: Char) { for (row in 1..rows) { for (col in 1..cols) { print(c) } println() } } drawBox(5, 3, '@') // should look like... // // @@@ // @@@ // @@@ // @@@ // @@@ // TODO 5/5: print a multiplication table fun multiplicationTableSimple(n: Int) { for (row in 1..n) { for (col in 1..n) { print("${row * col} ") } println() } } multiplicationTableSimple(4) // easier version... // 1 2 3 4 // 2 4 6 8 // 3 6 9 12 // 4 8 12 16 fun multiplicationTable(n: Int) { val maxStr = "${ n*n } " for (row in 1..n) { for (col in 1..n) { val toPrint = "${row * col} " print(toPrint) for (space in 1..(maxStr.length - toPrint.length)) { print(" ") } } println() } } multiplicationTable(4) // harder version... // 1 2 3 4 // 2 4 6 8 // 3 6 9 12 // 4 8 12 16