forked from irisyuan/Algorithm-Implementations
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Java implementation of Caesar Cipher algorithm
- Loading branch information
Iris Yuan
committed
Aug 14, 2014
1 parent
e441a91
commit 90b944e
Showing
1 changed file
with
31 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
import java.util.Scanner; | ||
|
||
public class caesarCipher { | ||
|
||
// cipher(String,int) returns original message shifted by 3 letters | ||
// if input is numerical, adds 3 to number | ||
public static char[] cipher(String message, int shift) { | ||
|
||
// declare variables | ||
String s = message; | ||
// convert string to array of chars | ||
char[] letters = message.toCharArray(); | ||
|
||
for(int i = 0; i < message.length(); i++){ | ||
letters[i] += 3; | ||
} | ||
return letters; | ||
} | ||
|
||
// main method takes user input and calls cipher | ||
public static void main(String[] args) { | ||
|
||
Scanner input = new Scanner(System.in); | ||
System.out.println("Plaintext: "); | ||
String message = input.nextLine(); | ||
|
||
System.out.println("Ciphertext:"); | ||
System.out.println(cipher(message, 3)); | ||
} | ||
|
||
} |