Ciphers
Fundamentals
func caesarCypher( _ input: String, amount: Int, reversed: Bool = false, encode: Bool = true, ignoreSpecialChars: Bool = false, matchCase: Bool = true ) -> String { let alphabet = Array("ABCDEFGHIJKLMNOPQRSTUVWXYZ") return input.map { character in let char = Character(character.uppercased()) guard alphabet.contains(char) else { return ignoreSpecialChars ? "" : String(char) } var newIndex = alphabet.firstIndex(of: char)! switch encode { case true: newIndex += (reversed ? -amount : amount) case false: newIndex += (reversed ? amount : -amount) } (newIndex < 0) ? (newIndex += 26) : () (newIndex >= 26) ? (newIndex -= 26) : () return (char.isLowercase && matchCase) ? alphabet[newIndex].lowercased() : alphabet[newIndex].uppercased() }.joined() }
- func caesarCypher(
- _ input: String,
- amount: Int,
- reversed: Bool = false,
- encode: Bool = true,
- ignoreSpecialChars: Bool = false,
- matchCase: Bool = true
- ) -> String {
- let alphabet = Array("ABCDEFGHIJKLMNOPQRSTUVWXYZ")
return input.map({ character in- return input.map { character in
- let char = Character(character.uppercased())
- guard alphabet.contains(char) else {
- return ignoreSpecialChars ? "" : String(char)
- }
- var newIndex = alphabet.firstIndex(of: char)!
if encode {newIndex += reversed ? -amount : amount} else {newIndex += reversed ? amount : -amount}if newIndex < 0 {newIndex += 26}if newIndex >= 26 {newIndex -= 26- switch encode {
- case true:
- newIndex += (reversed ? -amount : amount)
- case false:
- newIndex += (reversed ? amount : -amount)
- }
return char.isLowercase && matchCase? alphabet[newIndex].lowercased(): alphabet[newIndex].uppercased()}).joined()- (newIndex < 0) ? (newIndex += 26) : ()
- (newIndex >= 26) ? (newIndex -= 26) : ()
- return (char.isLowercase && matchCase)
- ? alphabet[newIndex].lowercased() : alphabet[newIndex].uppercased()
- }.joined()
- }
Ciphers
Fundamentals
A Caesar cypher (or cipher) is a substitution cipher where each letter in the input text is shifted by a fixed number of positions in the alphabet.
Parameters:
- input: The text to be encoded or decoded.
- amount: The number of positions to shift each character.
-
reversed: If
true
, the shift is applied to the left instead of the right (default isfalse
). -
encode: If
true
, the cipher encodes the input; iffalse
, it decodes it. -
ignoreSpecialChars: If
true
, characters not in the standard alphabet (e.g. spaces, punctuation) are excluded from the output. -
matchCase: If
true
, the output retains its original casing; iffalse
, the result will be all uppercase.
Returns:
The resulting encoded or decoded string.
func caesarCypher(
_ input: String,
amount: Int,
reversed: Bool = false,
encode: Bool = true,
ignoreSpecialChars: Bool = false,
matchCase: Bool = true
) -> String {
let alphabet = Array("ABCDEFGHIJKLMNOPQRSTUVWXYZ")
return input.map({ character in
let char = Character(character.uppercased())
guard alphabet.contains(char) else {
return ignoreSpecialChars ? "" : String(char)
}
var newIndex = alphabet.firstIndex(of: char)!
if encode {
newIndex += reversed ? -amount : amount
} else {
newIndex += reversed ? amount : -amount
}
if newIndex < 0 {
newIndex += 26
}
if newIndex >= 26 {
newIndex -= 26
}
return char.isLowercase && matchCase
? alphabet[newIndex].lowercased()
: alphabet[newIndex].uppercased()
}).joined()
}
import XCTest
class SolutionTest: XCTestCase {
static var allTests = [
("Fixed Tests", fixedTests),
]
func fixedTests() {
let input = "The Quick Brown Fox Jumps Over The Lazy Dog"
XCTAssertEqual(caesarCypher(input, amount: 11, ignoreSpecialChars: true, matchCase: false), "ESPBFTNVMCZHYQZIUFXADZGPCESPWLKJOZR")
}
}
XCTMain([
testCase(SolutionTest.allTests)
])