英文:
How to move 3 alphabets backwards in lowercase letter?
问题
我正在尝试在 Kotlin 中实现凯撒密码。在凯撒密码中,您通过将字母替换为字母表中某个固定位置的字母来加密消息。您可以查看这里获取更多信息,以及我采用的加密和解密方法。
我的加密正常工作。
注意:s 代表字母表中的位移量,默认为 3。
fun encrypt(message: String, s: Int = 3): String {
var encrypt: String = ""
for (m in message) {
if (m.isLetter() && m.isUpperCase())
encrypt += ((m + s - 65).toInt() % 26 + 65).toChar()
else if (m.isLetter() && m.isLowerCase())
encrypt += ((m + s - 97).toInt() % 26 + 97).toChar()
else
encrypt += m
}
return encrypt
}
对于解密,我不是加 3,而是减 3。但我在解密时遇到了一些问题,而且只有在小写字母中才存在问题。
这是我的解密代码:
fun decrypt(message: String, s: Int = 3): String {
var decrypt: String = ""
for (m in message) {
if (m.isLetter() && m.isUpperCase())
decrypt += ((m - s + 65).toInt() % 26 + 65).toChar()
else if (m.isLetter() && m.isLowerCase())
decrypt += ((m - s + 97).toInt() % 26 + 97).toChar()
else
decrypt += m
}
return decrypt
}
对于大写字母,我的输出是正确的:
J -> G
但对于小写字母,输出是不正确的。
j -> s
我已经使用这个图像来查看字符的十进制值。感谢您关注我的问题。
英文:
I am trying to implement Caesar Cipher in Kotlin. In Caesar Cipher you encrypt the message by substituting a letter with some fixed number of positions down the alphabet. You can look here for more information and also the approach I adopted for encryption and decryption.
My encryption works properly.
Note: s is for a shift in alphabets and I have kept it as 3 by default.
fun encrypt(message:String,s:Int=3):String{
var encrpt:String = ""
for(m in message){
if(m.isLetter() && m.isUpperCase())
encrpt+=((m + s - 65).toInt() % 26 + 65).toChar()
else if(m.isLetter() && m.isLowerCase())
encrpt+=((m + s - 97).toInt() % 26 + 97).toChar()
else
encrpt+=m
}
return encrpt
}
For decryption instead of adding 3, I am subtracting 3. But I am facing some problem in decryption and that too only for lower case letters.
This is my code for decryption:
fun decrypt(message:String,s:Int=3):String{
var decrpt:String = ""
for(m in message){
if(m.isLetter() && m.isUpperCase())
decrpt+=((m - s + 65).toInt() % 26 + 65).toChar()
else if(m.isLetter() && m.isLowerCase())
decrpt+=((m - s + 97).toInt() % 26 + 97).toChar()
else
decrpt+=m
}
return decrpt
}
For uppercase letters my output is fine:
J->G
But for lowercase letters, the output is incorrect.
j->s
I have used this image for the decimal values of characters. Thank you for looking into my problem.
答案1
得分: 1
好的,以下是翻译的部分:
"Okay, so I just noticed some instructions given by the author for decryption here at the end.
> we can use the same function to decrypt, instead we’ll modify the shift value such that shift = 26-shift
So now decryption function looks like this:
fun decrypt(message:String,s:Int=3):String{
var decrpt:String = ""
for(m in message){
if(m.isLetter() && m.isUpperCase())
decrpt+=((m + (26-s) - 65).toInt() % 26 + 65).toChar()
else if(m.isLetter() && m.isLowerCase())
decrpt+=((m + (26-s) - 97).toInt() % 26 + 97).toChar()
else
decrpt+=m
}
return decrpt
}
Still, I am curious to know if I can use the same shift in the opposite direction to decrypt the encrypted text.
英文:
Okay, so I just noticed some instructions given by the author for decryption here at the end.
> we can use the same function to decrypt, instead we’ll modify the shift value such that shift = 26-shift
So now decryption function looks like this:
fun decrypt(message:String,s:Int=3):String{
var decrpt:String = ""
for(m in message){
if(m.isLetter() && m.isUpperCase())
decrpt+=((m + (26-s) - 65).toInt() % 26 + 65).toChar()
else if(m.isLetter() && m.isLowerCase())
decrpt+=((m + (26-s) - 97).toInt() % 26 + 97).toChar()
else
decrpt+=m
}
return decrpt
}
Still, I am curious to know if I can use the same shift in the opposite direction to decrypt the encrypted text.
答案2
得分: 0
这里处理大写和小写解密的方式都不正确。减去 s
是正确的,但你还需要减去 65 或 97('a' 或 'A'),以便能够执行模运算:
fun decrypt(message: String, s: Int = 3): String {
var decrypt: String = ""
for (m in message) {
if (m.isLetter() && m.isUpperCase())
decrypt += ((m.toInt() - s - 65 + 26) % 26 + 65).toChar()
else if (m.isLetter() && m.isLowerCase())
decrypt += ((m.toInt() - s - 97 + 26) % 26 + 97).toChar()
else
decrypt += m
}
return decrypt
}
英文:
The handling of both the upper-case and lower-case decryption here is wrong. Subtracting s
is correct, but you need to also subtract 65 or 97 ('a' or 'A'), in order to be able to perform a modulu operation:
fun decrypt(message:String,s:Int=3):String{
var decrpt:String = ""
for(m in message){
if(m.isLetter() && m.isUpperCase())
decrpt+=((m - s - 65).toInt() % 26 + 65).toChar()
else if(m.isLetter() && m.isLowerCase())
decrpt+=((m - s - 97).toInt() % 26 + 97).toChar()
else
decrpt+=m
}
return decrpt
}
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论