Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- public class CaesarCipher {
- public static void main(String[] strings) {
- int offset = 0;
- char c;
- int shift= 0;;
- boolean isComplete = false;
- while (offset<=-25 || offset == 0 || offset >=25 || offset!=-999 || offset!=999) {
- TextIO.putln("Please enter the shift value (between -25..-1 and 1..25)");
- offset = TextIO.getlnInt();
- if (offset<=-25 || offset>=25 || offset == 0) {
- TextIO.putln(offset+" is not a valid shift value.");
- };
- }
- TextIO.putln("Using shift value of "+offset);
- while (isComplete == false) {
- TextIO.putln("Please enter the source text (empty line to quit)");
- String m= TextIO.getln();
- if (m.equals("")) {
- TextIO.putln("Bye.");
- isComplete =true;
- return;
- }
- TextIO.putln("Source :"+m);
- m = m.toUpperCase();
- String result ="";
- if (offset == 999) {
- boolean posishift = true;
- offset = 1;
- }
- if (offset == -999) {
- boolean negashift = true;
- offset = -1;
- }
- for (int i=0; i<=m.length() -1; i++ ) {
- c = m.charAt(i);
- if ( (int)c >=65 && (int)c <=90) {
- int letter = c - 'A';
- shift = (26 + letter + offset) % 26;
- char newletter = (char) (shift + 'A');
- result += newletter;
- }
- if (posishift) offset ++;
- if (negashift) offset --; else result += c;
- }
- TextIO.putln("Processed:"+result);
- }
- }
- }
Add Comment
Please, Sign In to add comment