A Caesar cipher (named after the Roman Emperor Julius
Caesar) is a rudimentary encoding scheme in which each letter is replaced by
the letter occurring k places
after it in the alphabet. (Imagine that the letters are listed in a circle so
that A immediately follows Z.) The number k is referred to as the shift because, in effect, a Caesar
cipher shifts all the letters of the alphabet k positions.
For example, if k = 2,
A would be encoded as C, B would be encoded as D, ..., X would be encoded as Z, Y would
be encoded as A, and Z would be encoded as B. The notion
of a negative shift makes sense, too. For example, if k = -3, A would be
encoded as X, B would be encoded as Y, C would be
encoded as Z, D would be encoded as A,
..., Z would be encoded as W.
Develop a program that reads pairs of lines from a data
file called cipher.txt. The first
value is a shift value (an integer on a line by itself) and the second value is
a message (a string of characters, also on a line by itself). The program
should read the shift value and the message and output the shift value and the
message (on separate lines) followed by the corresponding encoded form of the
message (also on a separate line, and followed by a blank line). The message
will be no longer than 40 characters. The shift value will be no less than -25
and no greater than 25. Any character that occurs in the message and that is
not an upper case letter should be encoded as itself. The program should repeat
until the value zero appears for the shift value. Note that this number will not be followed by a character string.
Name your class Cipherxxx.java where the "xxx" is your user name.
Sample Program Execution:
Input 7 ALL ABOARD 0 Output 7 -4 ALL ABOARD WHH
WXKWNZ 0 End of
program |
|