You are viewing a single comment's thread. Return to all comments →
Kotlin solution fun caesarCipher(s: String, k: Int): String { // Write your code here val ACode = 'A'.code val ZCode = 'Z'.code val aCode = 'a'.code val zCode = 'z'.code val shift = k.mod(ZCode-ACode+1) val newS = mutableListOf() val charA = s.toCharArray() for (i in 0..charA.size-1){ var symCode = charA[i].code if( symCode>= ACode && symCode <= ZCode){ symCode += shift if( symCode > ZCode) symCode = ACode - (ZCode - symCode + 1) } if( symCode>= aCode && symCode <= zCode){ symCode += shift if( symCode > zCode) symCode = aCode - (zCode - symCode + 1) } newS.add( symCode.toChar()) } return String(newS.toCharArray()) }
Seems like cookies are disabled on this browser, please enable them to open this website
Caesar Cipher
You are viewing a single comment's thread. Return to all comments →
Kotlin solution fun caesarCipher(s: String, k: Int): String { // Write your code here val ACode = 'A'.code val ZCode = 'Z'.code val aCode = 'a'.code val zCode = 'z'.code val shift = k.mod(ZCode-ACode+1) val newS = mutableListOf() val charA = s.toCharArray() for (i in 0..charA.size-1){ var symCode = charA[i].code if( symCode>= ACode && symCode <= ZCode){ symCode += shift if( symCode > ZCode) symCode = ACode - (ZCode - symCode + 1) } if( symCode>= aCode && symCode <= zCode){ symCode += shift if( symCode > zCode) symCode = aCode - (zCode - symCode + 1) } newS.add( symCode.toChar()) } return String(newS.toCharArray()) }