- 01Getting Started
- 02Your First Output (print)
- 03Working with Variables
- 04Conditionals (if statements)
- 05Loops (for-in statements)
- 06Writing a Function
- 07Working with Arrays
- 08Loops (while statements)
- 09Classes (Object-Oriented Programming)
- 10Error Handling (do-try-catch)
- 11Sorting an Array
- 12Recursive Functions
- 13Using the Standard Library
- 14The switch Statement
- 15The Ternary Operator
- 16Inheritance (Extending a Class)
- 17Writing Comments
- 18Logical Operators (&&, ||, !)
- 19Constants (let, and why it matters)
- 20Splitting and Joining Strings (split, joined)
- 21Optionals (Writing nil-Safe Code)
- 22Searching an Array (contains, first)
- 23Transforming an Array (map)
- 24Two-Dimensional Arrays (Tabular Data)
- 25Writing a Custom Error Type
- 26Default Parameter Values
- 27Working with Set
- 28Checking Correctness (Your First Step into Testing)
- 29Higher-Order Functions (Passing a Function as an Argument)
- 30Stacks and Queues (Basic Data Structures)
- 31Type Conversion (Casting) Basics
- 32Intro to Regular Expressions (Pattern Matching)
- 33The Binary Search Algorithm
- 34Building a Caesar Cipher (a Character-Shifting Cipher)
- 35Understanding How Bubble Sort Works
- 36Building and Displaying Dates (Basic Year/Month/Day Operations)
- 37Writing Multiple Test Cases Together
- 38Speeding Up Calculations with Memoization (Caching)
- 39Normalizing Strings (trimmingCharacters, Unifying Case)
- 40Shallow Copy vs. Deep Copy
- 41Enum (Enumerated Types) Basics
- 42Flattening an Array
- 43Reversing a String and Checking for Palindromes
- 44Pairing Up Two Arrays (the zip operation)
- 45Rounding Numbers (rounded)
- 46Multi-Line Strings (Triple Quotes)
- 47Functions That Return Multiple Values (Tuples)
- 48Finding the GCD and LCM (the Euclidean Algorithm)
- 49Formatting Numbers (Padding Digits, Decimal Places)
- 50Cleanup Logic with defer
- 51Writing Generic Functions
- 52File Reading and Writing Basics
- 53Generating Random Numbers
- 54Bitwise Operators (AND, OR, XOR, shifts)
- 55Reading Command-Line Arguments
- 56Waiting for a Fixed Amount of Time (Thread.sleep)
- 57Watch Out for Floating-Point Rounding Errors
- 58Reading from Standard Input
- 59FizzBuzz (the Classic Practice Problem)
- 60Checking Whether a Number Is Prime
- 61Set Operations with Set (Union, Intersection, Difference)
- 62Converting Number Bases (Binary, Hex)
- 63Checking Balanced Parentheses (an Application of Stacks)
- 64Checking Whether Two Words Are Anagrams
- 65Checking Whether a Year Is a Leap Year
- 66Converting Temperature (Celsius to Fahrenheit)
- 67Prime Factorization
- 68[Applied] Build a Simple Inventory Management Tool
Building a Caesar Cipher (a Character-Shifting Cipher)
In this lesson you will learn how to implement a Caesar cipher in Swift, and understand the basic idea of encrypting by shifting characters. This is for anyone searching for "Swift Caesar cipher implementation".
The Caesar cipher is a simple cipher from ancient Rome that replaces each letter with the one a fixed number of positions further along the alphabet. It is the same idea as a childhood game of shifting every letter by three.
The sample code converts characters to Unicode scalar values with text.unicodeScalars and, checking whether each is in the alphabet, computes the shifted letter with (c.value - 65 + UInt32(shift)) % 26 + 65. Notice how anything that is not a letter is passed through unchanged.
A common stumbling block for beginners is the character-code arithmetic. You need % 26 to handle the case where the shifted result runs past the end of the alphabet (A to Z).
It is very simple next to modern cryptography, but it is an ideal exercise for practising character-code arithmetic.
๐งช This site can't compile or run Swift directly, so it checks on the spot whether what you typed matches the reference code (scoring happens entirely in your browser โ nothing is sent anywhere).
