Advertisement
UME14

Caesar/PSet2/CS50

Dec 19th, 2018
159
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.29 KB | None | 0 0
  1. #include <cs50.h>
  2. #include <ctype.h>
  3. #include <stdio.h>
  4. #include <stdlib.h>
  5. #include <string.h>
  6.  
  7. int main(int argc, string argv[])
  8. {
  9. //the key
  10. int key;
  11. //key = atoi(argv[1]);
  12.  
  13.  
  14. //taking key value
  15. if (argc == 2)
  16. {
  17. key = atoi(argv[1]);
  18. }
  19. else
  20. {
  21. //nothing importsnt. just for later use -_-
  22. //but also helps with no command line argument
  23. //key = get_int("Enter key: ");
  24. return 1;
  25. }
  26.  
  27. string pt = get_string("plaintext: ");
  28. char ct[strlen(pt) + 1];
  29.  
  30. //so it will round to A, not some / or [
  31. key = (key % 26);
  32.  
  33. for (int i = 0, n = strlen(pt); i < n; i++)
  34. {
  35. if (isalpha(pt[i]))
  36. {
  37. //checking if inside the Alphabet bound
  38.  
  39. if (((pt[i] + key) > 64 && (pt[i] + key) < 91) || ((pt[i] + key) > 96 && (pt[i] + key) < 123))
  40. {
  41. ct[i] = pt[i] + key;
  42. }
  43. else
  44. {
  45. //taking care of out of out of bound
  46. ct[i] = (pt[i] + key) - 26;
  47. }
  48. }
  49. else
  50. {
  51. ct[i] = pt[i];
  52. }
  53.  
  54. ct[strlen(pt)] = '\0';
  55.  
  56. }
  57. //printf("ciphertext: ");
  58. printf("%s%s\n", "ciphertext: ", ct);
  59.  
  60. return 0;
  61. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement