how use functional programming in scala function
Solution 1:
String is effectively a collection of char
's so you can treat it like one. For example:
val result = text
.map(c => if (voyelle.contains(c)) "av" + c else c.toString)
.mkString
Solution 2:
How about
text.replaceAll("([aeiou])", "av$1")
Solution 3:
You can just use flatMap
def translate(text: String): String = {
val vowels = Set('a', 'e', 'i', 'o', 'u')
text.trim.toLowerCase.flatMap { c =>
if (vowels.contains(c)) s"av${c}"
else c.toString
}
}
code running here.