-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtutorial_6.scala
More file actions
33 lines (23 loc) · 756 Bytes
/
Copy pathtutorial_6.scala
File metadata and controls
33 lines (23 loc) · 756 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
object Tutorial_6 {
def main(args: Array[String]) = {
print("Enter a phrase to encrypt: ");
val str: String = scala.io.StdIn.readLine();
val encrypted_str = cipher(str)(encrypt);
println("Encrypted Phrase: " + encrypted_str);
val decrypted_str = cipher(encrypted_str)(decrypt);
println("Decrypted Phrase: " +decrypted_str);
}
def encrypt(str: String) = {
var charArray = str.toArray;
for (i <- 0 to (charArray.length-1))
charArray(i) = (charArray(i)+1).toChar;
charArray.mkString;
}
def decrypt(str: String) = {
var charArray = str.toArray;
for (i<- 0 to (charArray.length-1))
charArray(i) = (charArray(i)-1).toChar;
charArray.mkString;
}
def cipher(str: String)(callback: String => String) = callback(str);
}