Caesar Cipher - spoiler alert

  • there is no Swift projects sub-heading, so I labeled it JS and workaround

I was not sure where to post this, but I don’t need help ( but am ALWAYS open to suggestions and learning something new!). Just wanted to share a quick add-on I made to the Caesar Cipher project in the Swift course ( awesome course! thank you @sonnynomnom)

So the course project objective is to make a cipher out of a secret message that you want to pass on to someone. I added a way to also decode the same message. No stroke of genius, but I think if this were a real program someone needed it would be a helpful feature :slight_smile:

var alphabet: [Character] = ["a", "b", "c", "d", "e", "f", "g", "h", "i", "j", "k", "l", "m", "n", "o", "p", "q", "r", "s", "t", "u", "v", "w", "x", "y", "z"]

var secretMessage = "do you want to play a game?"
// print(secretMessage)
print("")
var message = Array(secretMessage)
//print(message)
print("")

print("Begin cipher ->")
print("")
for i in 0 ..< message.count {
  //print(i)
  for j in 0 ..< alphabet.count {
    //print(j)
    if message[i] == alphabet[j] {
      message[i] = alphabet[(j+3) % 26]
      break
    }
  }
}
print(message)
print("")

print("Begin decoded message ->")
print("")
for k in 0 ..< message.count {
  for l in 0 ..< alphabet.count {
    if message[k] == alphabet[l] {
    message[k] = alphabet[(l + 23) % 26]
    break
    }
  }
}
print(message)