How to reverse each word in a string with Scala

1 Answer

0 votes
object ReverseEachWord {
  def reverseEachWordInString(s: String): String = {
    val words = s.split(" ")
    val reversedWords = words.map(word => word.reverse)
    
    reversedWords.mkString(" ")
  }

  def main(args: Array[String]): Unit = {
    val s = "scala java c++ rust python c#"
    
    println(reverseEachWordInString(s))
  }
}




/*
run:
    
alacs avaj ++c tsur nohtyp #c
    
*/

 



answered Aug 31, 2024 by avibootz
...