I am working on Monoalphabetic Cipher and I have to encrypt a message by substituting it with any other alphabet.
fun encrypt(message:String){
//Assume message as ABCD
var encrypt:String = ""
val alphabets = ('A'..'Z').toMutableList() //['A','B','C','D',....]
var shuffAlp = alphabets.shuffled() // Assume shuffled op as ['D','L','N','J',....]
var pair = alphabets.zip(shuffAlp)
println(pair) // Assume pair as [(A, D), (B, L), (C, N), (D, J), ...] acc to shuffled op
for(m in message){
for (p in pair){
if(p.first == m)
encrypt+=p.second
}
}
println(encrypt) //DLNJ according to given pair
}
As you can see I have used two for loops to generate my encrypt String. I was wondering if there is an equivalent Lambda function or extensions that would discard two loops?