Caesar's Cipher Swift

Caesar’s Cipher (Swift)

I’m confused where (why?) we got the I and the j for our for -in loops. Hopefully I will figure it out (and come back here and let you know) by the time I finish, but I feel like I wanna make sure I understand. Did we use i and j and start at 0 as though it’s the beginning of the array we are looping through?

Thank you. ~Teraisa

Hello! Could you please post the code you’re wondering about? To format code, press this button:


and insert the code between the back ticks:Screen Shot 2020-05-02 at 14.06.13

Ha, ha, the link was just to the exercise, where it says to use “I” and then to use “j” but here’s my code. Basically, wondering if there’s a reason why we chose those letters–random? Special Swift variables for Character? Because our “hello” begins with “h”? Thank you!

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 = "hello"

var message = Array(secretMessage)

for **i** in 0 ..< message.count{
  for **j** in 0 ..<alphabet.count{
    if message[i] == alphabet[j]{
      message[i] = alphabet[j+3]
      break
    }
  }
}
print(message)
1 Like

Hello! Those letters are just variables. They could be any valid variable name.

2 Likes