-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcaesar.java
More file actions
35 lines (30 loc) · 1.09 KB
/
caesar.java
File metadata and controls
35 lines (30 loc) · 1.09 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
package com.company;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
System.out.println("\ncaesar algorithm implementation : enter the number\t");
int number = scanner.nextInt();
System.out.println("\nenter the word to encode : ");
String word = scanner.next();
String buffer = caesarEncode(word, number);
System.out.println(buffer);
}
public static String caesarEncode(String word, int n) {
StringBuffer result = new StringBuffer();
for (int i = 0; i < word.length(); i++) {
if (Character.isUpperCase(word.charAt(i))) {
char ch = (char) (((int) word.charAt(i) +
n - 65) % 26 + 65);
result.append(ch);
} else {
char ch = (char) (((int) word.charAt(i) +
n - 97) % 26 + 97);
result.append(ch);
}
}
return result.toString();
}
}