Advertisement
Guest User

Untitled

a guest
May 12th, 2016
79
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.94 KB | None | 0 0
  1. /**
  2. * pset 2
  3. * Caesar's Cipher
  4. *
  5. * Mike
  6. *
  7. * Scrambles a message by k characters
  8. */
  9.  
  10. #include <stdio.h>
  11. #include <cs50.h>
  12. #include <stdlib.h>
  13. #include <string.h>
  14. #include <ctype.h>
  15.  
  16. int main(int argc, string argv[])
  17. {
  18. if(argc != 2 || atoi(argv[1]) < 1)
  19. {
  20. printf("error\n");
  21. return 1;
  22. }
  23.  
  24. // get k
  25.  
  26. int k = atoi(argv[1]);
  27. int i;
  28.  
  29.  
  30. // get message
  31.  
  32. string scramble = GetString();
  33.  
  34. // scramble and output
  35.  
  36. for(i = 0; i < strlen(scramble); i++)
  37. {
  38. //lowercase
  39. if(isupper(scramble[i]))
  40. {
  41. int code = (scramble[i] - 65 + k) %26;
  42. printf("%c", (char) code + 65);
  43. }
  44. //uppercase
  45. else if(islower(scramble[i]))
  46. {
  47. int code = (scramble[i] - 97 + k) %26;
  48. printf("%c", (char) code + 97);
  49. }
  50. else (printf("%c", scramble[i]));
  51. }
  52. printf("\n");
  53. return 0;
  54. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement