Skip to content

Commit 7946f03

Browse files
authored
Update create-a-caesar-cipher-with-java.mdx
1 parent 5bbcf24 commit 7946f03

File tree

1 file changed

+9
-9
lines changed

1 file changed

+9
-9
lines changed

projects/create-a-ceasar-cipher-with-java/create-a-caesar-cipher-with-java.mdx

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,7 @@ So, he pulled off a kinda genius move... he shifted each letter in his messages
4444

4545
This meant that "attack at dawn" would become "dwwdfn dw gdzq" to any nosey enemies.
4646

47-
```terminal
47+
```
4848
Wms fytc y jckmlybc kmsrf!
4949
```
5050

@@ -54,7 +54,7 @@ The text above contains an _encrypted_ message, and more specifically, a Caesar
5454

5555
For example, let's say we shift the letters by 3:
5656

57-
```terminal
57+
```
5858
A → D
5959
B → E
6060
C → F
@@ -117,7 +117,7 @@ for (int i = 0; i < chars.length; i++) {
117117

118118
We are using the `.toCharArray()` method to turn our user's text into a char array to loop through it. This is what you would have if the value of `text` was "hello":
119119

120-
```terminal
120+
```
121121
['h', 'e', 'l', 'l', 'o'];
122122
```
123123

@@ -143,7 +143,7 @@ char base = Character.isLowerCase(character) ? 'a' : 'A';
143143

144144
This `if`/`else` shorthand (`?` `:`) called the ternary operator provides either the `a` or `A` character depending if the letter is upper case or lower case.
145145

146-
```terminal
146+
```
147147
condition ? valueIfTrue : valueIfFalse
148148
```
149149

@@ -171,24 +171,24 @@ Let's say we are trying to shift `C` 3 spaces. Since 'C' is uppercase, the base
171171

172172
If we subtract the base from the character, we get:
173173

174-
```terminal
174+
```
175175
character - base = 'C' - 'A' = 67 - 65 = 2
176176
```
177177
So 'C' is the 2nd letter in the alphabet (index 2, starting from 0). Now we have the index of our letter, but since we're trying to shift it 3 spaces, let's add `3` to the current index.
178178

179-
```terminal
179+
```
180180
(character - base + shift)
181181
```
182182

183183
Let's also make sure that it _wraps_ around the alphabet, so `Z` shifted 3 spaces makes it to `C`. We can use the `%` modulo.
184184

185-
```terminal
185+
```
186186
(character - base + shift) % 26
187187
```
188188

189189
Since we have a new index now, we can add our base to turn it _back_ to an ASCII value to turn it into a new letter!
190190

191-
```terminal
191+
```
192192
(character - base + shift) % 26 + base
193193
(67 - 65 + 3) % 26 + 3 = 70
194194
```
@@ -242,7 +242,7 @@ You'll need two things from the user:
242242
- text to encrypt (String)
243243
- the shift number (0-25) (int)
244244

245-
```terminal
245+
```
246246
Enter text to encrypt: Thank you Penguin123 for keeping Club Penguin safe. Here are four hundred coins.
247247
Enter shift key (0-25): 17
248248
```

0 commit comments

Comments
 (0)