You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: projects/create-a-caesar-cipher-with-java/create-a-caesar-cipher-with-java.mdx
+20-16Lines changed: 20 additions & 16 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -44,7 +44,7 @@ So, he pulled off a kinda genius move... he shifted each letter in his messages
44
44
45
45
This meant that "attack at dawn" would become "dwwdfn dw gdzq" to any nosey enemies.
46
46
47
-
```
47
+
```output
48
48
Wms fytc y jckmlybc kmsrf!
49
49
```
50
50
@@ -54,7 +54,7 @@ The text above contains an _encrypted_ message, and more specifically, a Caesar
54
54
55
55
For example, let's say we shift the letters by 3:
56
56
57
-
```
57
+
```output
58
58
A → D
59
59
B → E
60
60
C → F
@@ -72,6 +72,8 @@ Let's build a program that lets you create your own cipher!
72
72
73
73
74
74
## Setup
75
+
Choose your Java IDE or [Codédex Builds](https://www.codedex.io/builds), and create a new Java project.
76
+
75
77
Before we begin, be sure to use the correct import for the `Scanner`, since we will be utilizing user input.
76
78
77
79
```java
@@ -102,7 +104,9 @@ Consider the sentence: "For each character in this sentence we want to do x". We
102
104
103
105
```java
104
106
// for-each loop
105
-
for (char character : text.toCharArray()) {}
107
+
for (char character : text.toCharArray()) {
108
+
// do something with character
109
+
}
106
110
```
107
111
108
112
Which is exactly the same as doing:
@@ -171,7 +175,7 @@ Let's say we are trying to shift `C` 3 spaces. Since 'C' is uppercase, the base
171
175
172
176
If we subtract the base from the character, we get:
173
177
174
-
```
178
+
```terminal
175
179
character - base = 'C' - 'A' = 67 - 65 = 2
176
180
```
177
181
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.
@@ -219,16 +223,16 @@ Then, be sure to `return` the final string result. Your method should now look l
219
223
publicstaticString encrypt(String text, int shift) {
220
224
StringBuilder result =newStringBuilder();
221
225
222
-
for (char character : text.toCharArray()) {
223
-
if (Character.isLetter(character)) {
224
-
char base =Character.isLowerCase(character) ?'a':'A';
@@ -242,7 +246,7 @@ You'll need two things from the user:
242
246
- text to encrypt (String)
243
247
- the shift number (0-25) (int)
244
248
245
-
```
249
+
```terminal
246
250
Enter text to encrypt: Thank you Penguin123 for keeping Club Penguin safe. Here are four hundred coins.
247
251
Enter shift key (0-25): 17
248
252
```
@@ -270,7 +274,7 @@ Your project is now ready to be tested, try encrypting your favorite movie quote
270
274
271
275
Congratulations!!! 🎊
272
276
273
-
You've reached the end of the project, and now you have a cool encrypter to mess around with!
277
+
You've reached the end of the project, and now you have a cool encrypter to mess around with! Find the solution code [here](https://github.com/codedex-io/projects/blob/main/projects/create-a-caesar-cipher-with-java/CaesarCipher.java)
0 commit comments