object PassStrength {
  def main(args: Array[String]): Unit = {
    println("Enter a password:")
    val password = scala.io.StdIn.readLine()

    val (strength, score) = checkPasswordStrength(password)

    strength match {
      case PasswordStrength.Weak => println(s"Weak password! Score: $score%")
      case PasswordStrength.Medium => println(s"Medium password. Score: $score%")
      case PasswordStrength.Strong => println(s"Strong password! Score: $score%")
    }
  }

  def checkPasswordStrength(password: String): (PasswordStrength.Value, Int) = {
    val lengthScore = calculateLengthScore(password)
    val characterScore = calculateCharacterScore(password)
    val score = (lengthScore + characterScore) / 2

    if (score < 40) {
      (PasswordStrength.Weak, score)
    } else if (score < 70) {
      (PasswordStrength.Medium, score)
    } else {
      (PasswordStrength.Strong, score)
    }
  }

  def calculateLengthScore(password: String): Int = {
    val minLength = 8
    val maxLength = 12
    val passLen = password.length

    if (passLen < minLength) {
      0
    } else if (passLen > maxLength) {
      100
    } else {
      val lengthRange = maxLength - minLength
      val lengthScore = ((passLen - minLength) / lengthRange.toDouble) * 100
      lengthScore.toInt
    }
  }

  def calculateCharacterScore(password: String): Int = {
    val uppercaseScore = calculateUppercaseScore(password)
    val lowercaseScore = calculateLowercaseScore(password)
    val digitScore = calculateDigitScore(password)
    val specialCharScore = calculateSpecialCharScore(password)
    val characterScore = (uppercaseScore + lowercaseScore + digitScore + specialCharScore) / 4
    characterScore.toInt
  }

  def calculateUppercaseScore(password: String): Int = {
    val uppercaseCount = password.count(_.isUpper)
    val score = (uppercaseCount.toDouble / password.length) * 100
    score.toInt
  }

  def calculateLowercaseScore(password: String): Int = {
    val lowercaseCount = password.count(_.isLower)
    val score = (lowercaseCount.toDouble / password.length) * 100
    score.toInt
  }

  def calculateDigitScore(password: String): Int = {
    val digitCount = password.count(_.isDigit)
    val score = (digitCount.toDouble / password.length) * 100
    score.toInt
  }

  def calculateSpecialCharScore(password: String): Int = {
    val specialCharCount = password.count(!_.isLetterOrDigit)
    val score = (specialCharCount.toDouble / password.length) * 100
    score.toInt
  }

  object PasswordStrength extends Enumeration {
    val Weak, Medium, Strong = Value
  }
}
 
by

Scala Online Compiler

Write, Run & Share Scala code online using OneCompiler's Scala online compiler for free. It's one of the robust, feature-rich online compilers for Scala language, running on the latest version 2.13.8. Getting started with the OneCompiler's Scala compiler is simple and pretty fast. The editor shows sample boilerplate code when you choose language as Scala and start coding.

Read input from STDIN in Scala

OneCompiler's Scala online editor supports stdin and users can give inputs to programs using the STDIN textbox under the I/O tab. Following is a sample Scala program which takes name as input and prints hello message with your name.

object Hello {
	def main(args: Array[String]): Unit = {
	  val name = scala.io.StdIn.readLine()        // Read input from STDIN
    println("Hello " + name ) 
	}
}

About Scala

Scala is both object-oriented and functional programming language by Martin Odersky in the year 2003.

Syntax help

Variables

Variable is a name given to the storage area in order to identify them in our programs.

var or val Variable-name [: Data-Type] = [Initial Value];

Loops and conditional statements

1. If family:

If, If-else, Nested-Ifs are used when you want to perform a certain set of operations based on conditional expressions.

If

if(conditional-expression){    
//code    
} 

If-else

if(conditional-expression) {  
//code if condition is true  
} else {  
//code if condition is false  
} 

Nested-If-else

if(condition-expression1) {  
//code if above condition is true  
} else if (condition-expression2) {  
//code if above condition is true  
}  
else if(condition-expression3) {  
//code if above condition is true  
}  
...  
else {  
//code if all the above conditions are false  
}  

2. For:

For loop is used to iterate a set of statements based on a criteria.

for(index <- range){  
  // code  
} 

3. While:

While is also used to iterate a set of statements based on a condition. Usually while is preferred when number of iterations are not known in advance.

while(condition) {  
 // code 
}  

4. Do-While:

Do-while is also used to iterate a set of statements based on a condition. It is mostly used when you need to execute the statements atleast once.

do {
  // code 
} while (condition) 

Functions

Function is a sub-routine which contains set of statements. Usually functions are written when multiple calls are required to same set of statements which increases re-usuability and modularity.

def functionname(parameters : parameters-type) : returntype = {   //code
}

Note:

You can either use = or not in the function definition. If = is not present, function will not return any value.