TopSecret


SUBMITTED BY: uuuuuu

DATE: Oct. 28, 2016, 7:07 p.m.

FORMAT: Text only

SIZE: 1.8 kB

HITS: 616

  1. //UIUC CS125 SPRING 2016 MP. File: CaesarCipher.java, CS125 Project: Challenge3-TopSecret, Version: 2016-02-15T06:55:07-0600.622398463
  2. /**
  3. * A program to search for to encrypt and decrypt lines of text. See
  4. * CaesarCipher.txt
  5. * Hints: line.charAt( int ) is useful.
  6. * You'll need loops, and conditions and variables
  7. * You'll need to read the Test cases to understand how your program should work.
  8. * Good Programming Hints: "DRY: Don't Repeat Yourself"
  9. * Try to make your program as short as possible.
  10. * TODO: add your netid to the line below
  11. * @author kosvick2
  12. */
  13. public class CaesarCipher {
  14. public static void main(String[] strings) {
  15. boolean done=true;
  16. boolean fun=true;
  17. while(done){
  18. TextIO.putln("Please enter the shift value (between -25..-1 and 1..25)");
  19. int shift = TextIO.getlnInt();
  20. if(shift>=-25 && shift<=25 && shift!= 0) {
  21. TextIO.putln("Using shift value of " +shift);
  22. while(fun){
  23. TextIO.putln("Please enter the source text (empty line to quit)");
  24. String source = TextIO.getln();
  25. if(source.length()==0){
  26. TextIO.putln("Bye.");
  27. fun=false;
  28. done=false;
  29. }
  30. else {
  31. TextIO.putln("Source :"+source);
  32. TextIO.put("Processed:");
  33. source = source.toUpperCase();
  34. char[] decode = source.toCharArray();
  35. for(int i=0; i<source.length(); i++){
  36. if((decode[i] >= 'A') && (decode[i] <= 'Z') && (decode[i]!= ' ')){
  37. decode[i] += shift;
  38. if((((decode[i]-'A')<0) || ((decode[i]-'A')<= 'Z')) && decode[i]!= ' ') {
  39. decode[i]= (char)((decode[i] - 'A' + 26)%26 + 'A');
  40. }
  41. }
  42. TextIO.put(decode[i]);
  43. }
  44. }
  45. }
  46. }
  47. else {
  48. TextIO.putln(shift+ " is not a valid shift value.");
  49. }
  50. }
  51. }
  52. }

comments powered by Disqus