Skip to content

Instantly share code, notes, and snippets.

@akwasiio
Last active October 4, 2023 01:12
Show Gist options
  • Save akwasiio/2c72c12aafb73701a75107e36394e486 to your computer and use it in GitHub Desktop.
Save akwasiio/2c72c12aafb73701a75107e36394e486 to your computer and use it in GitHub Desktop.
Simple pig latin implementation
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