How to reverse the order of the words in a string with Scala

1 Answer

0 votes
object ReverseWords extends App {
  val s = "aaa bbb ccc ddd eee";
  
  val reversedStringWords = reverseWords(s)
  
  println(reversedStringWords)

  def reverseWords(str: String): String = {
    val words = str.split(" ")
    
    val reversed = words.reverse.mkString(" ")
    
    reversed
  }
}

   
   
/*
run:

eee ddd ccc bbb aaa
 
*/

 



answered Feb 6, 2025 by avibootz
...