Last active
October 4, 2023 01:12
-
-
Save akwasiio/2c72c12aafb73701a75107e36394e486 to your computer and use it in GitHub Desktop.
Simple pig latin implementation
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
fun Char.isVowel(): Boolean { | |
return "aeiou".contains(lowercaseChar()) | |
} | |
fun String.toPigLatin(): String { | |
if (isEmpty()) return this | |
if (this[0].isVowel()) { | |
return this + "way" | |
} | |
var index = -1 | |
// find first vowel occurrence | |
for (i in this.indices) { | |
if (this[i].isVowel()) { | |
index = i | |
break | |
} | |
} | |
return if (index == -1) return this | |
else substring(index) + substring(0, index) + "ay" | |
} | |
fun main() { | |
println("human".toPigLatin()) //begins with a consonant | |
println("egg".toPigLatin()) //begins with a vowel | |
println("mist".toPigLatin()) //begins with a consonant | |
println("cricket".toPigLatin()) //consonant cluster word | |
println("extreme".toPigLatin()) //begins with a vowel | |
println("stress".toPigLatin()) //consonant cluster word | |
println("unit".toPigLatin()) //contains all vowels | |
println("Psst".toPigLatin()) //contains no vowel | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment