Welcome to collectivesolver - Programming & Software Q&A with code examples. A website with trusted programming answers. All programs are tested and work.

Contact: aviboots(AT)netvision.net.il

Buy a domain name - Register cheap domain names from $0.99 - Namecheap

Scalable Hosting That Grows With You

Secure & Reliable Web Hosting, Free Domain, Free SSL, 1-Click WordPress Install, Expert 24/7 Support

Semrush - keyword research tool

Boost your online presence with premium web hosting and servers

Disclosure: My content contains affiliate links.

39,845 questions

51,766 answers

573 users

How to find the longest and the shortest word in a string with Scala

2 Answers

0 votes
object Main extends App {
  val s = "scala php python c++ c c# java"
   
  var maxs = ""
  var mins = s
   
  val wordsarr = s.split(" ")
   
  for (word <- wordsarr) {
    if (word.length > maxs.length)
      maxs = word
    if (word.length < mins.length)
      mins = word
  }
   
  println(s"Longest word: $maxs")
  println(s"Shortest word: $mins")
}
 
 
 
/*
run:
 
Longest word: python
Shortest word: c
 
*/

 



answered Oct 10, 2024 by avibootz
edited Oct 10, 2024 by avibootz
0 votes
object Main extends App {
  val s = "scala php python c++ c c# java"
   
  val wordsarr = s.split(" ")
   
  var maxs = wordsarr.maxBy(_.length)
  var mins = wordsarr.minBy(_.length)
   
  println(s"Longest word: $maxs")
  println(s"Shortest word: $mins")
}
 
 
 
/*
run:
 
Longest word: python
Shortest word: c
 
*/

 



answered Oct 10, 2024 by avibootz

Related questions

...