Skip to content

Commit f08551c

Browse files
committed
Update PT from Feedback
1 parent de90169 commit f08551c

File tree

2 files changed

+53
-16
lines changed

2 files changed

+53
-16
lines changed
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
import java.util.Scanner;
2+
3+
public class CaesarCipher {
4+
5+
public static String encrypt(String text, int shift) {
6+
StringBuilder result = new StringBuilder();
7+
8+
for (char character : text.toCharArray()) {
9+
if (Character.isLetter(character)) {
10+
char base = Character.isLowerCase(character) ? 'a' : 'A';
11+
char shifted = (char) ((character - base + shift) % 26 + base);
12+
result.append(shifted);
13+
} else {
14+
result.append(character);
15+
}
16+
}
17+
18+
return result.toString();
19+
}
20+
21+
public static void main(String[] args) {
22+
Scanner scanner = new Scanner(System.in);
23+
24+
System.out.print("Enter text to encrypt: ");
25+
String inputText = scanner.nextLine();
26+
27+
System.out.print("Enter shift key (0-25): ");
28+
int shiftKey = scanner.nextInt();
29+
30+
String encrypted = encrypt(inputText, shiftKey);
31+
System.out.println("Encrypted text: " + encrypted);
32+
}
33+
}

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

Lines changed: 20 additions & 16 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-
```
47+
```output
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-
```
57+
```output
5858
A → D
5959
B → E
6060
C → F
@@ -72,6 +72,8 @@ Let's build a program that lets you create your own cipher!
7272

7373

7474
## Setup
75+
Choose your Java IDE or [Codédex Builds](https://www.codedex.io/builds), and create a new Java project.
76+
7577
Before we begin, be sure to use the correct import for the `Scanner`, since we will be utilizing user input.
7678

7779
```java
@@ -102,7 +104,9 @@ Consider the sentence: "For each character in this sentence we want to do x". We
102104

103105
```java
104106
// for-each loop
105-
for (char character : text.toCharArray()) {}
107+
for (char character : text.toCharArray()) {
108+
// do something with character
109+
}
106110
```
107111

108112
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
171175

172176
If we subtract the base from the character, we get:
173177

174-
```
178+
```terminal
175179
character - base = 'C' - 'A' = 67 - 65 = 2
176180
```
177181
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
219223
public static String encrypt(String text, int shift) {
220224
StringBuilder result = new StringBuilder();
221225

222-
for (char character : text.toCharArray()) {
223-
if (Character.isLetter(character)) {
224-
char base = Character.isLowerCase(character) ? 'a' : 'A';
225-
char shifted = (char) ((character - base + shift) % 26 + base);
226-
result.append(shifted);
227-
} else {
228-
result.append(character);
229-
}
230-
}
231-
226+
for (char character : text.toCharArray()) {
227+
if (Character.isLetter(character)) {
228+
char base = Character.isLowerCase(character) ? 'a' : 'A';
229+
char shifted = (char) ((character - base + shift) % 26 + base);
230+
result.append(shifted);
231+
} else {
232+
result.append(character);
233+
}
234+
}
235+
232236
return result.toString();
233237
}
234238
```
@@ -242,7 +246,7 @@ You'll need two things from the user:
242246
- text to encrypt (String)
243247
- the shift number (0-25) (int)
244248

245-
```
249+
```terminal
246250
Enter text to encrypt: Thank you Penguin123 for keeping Club Penguin safe. Here are four hundred coins.
247251
Enter shift key (0-25): 17
248252
```
@@ -270,7 +274,7 @@ Your project is now ready to be tested, try encrypting your favorite movie quote
270274

271275
Congratulations!!! 🎊
272276

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)
274278

275279
<ImageZoom src="https://i.imgur.com/IYCu7c4.gif" />
276280

0 commit comments

Comments
 (0)