Advertisement
Guest User

Untitled

a guest
Aug 14th, 2019
138
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.54 KB | None | 0 0
  1. #include <cs50.h>
  2. #include <stdio.h>
  3. #include <string.h>
  4. #include <ctype.h>
  5.  
  6. int main(int argc, string argv[])
  7.  
  8. {
  9. char *key = argv[1];
  10. char n;
  11. int newkey;
  12.  
  13. //rejects too many arguments
  14. if (argc != 2)
  15. {
  16. printf("./caesar key\n");
  17. return 1;
  18. }
  19. // converts char key into int newkey
  20. for (int i = 0; n = strlen(key), i < n; i++)
  21. {
  22. if isdigit(key[i])
  23. {
  24. newkey = atoi(key);
  25.  
  26. }
  27. // rejects letters
  28. else
  29. {
  30. printf("./caesar key\n");
  31. return 1;
  32. }
  33. }
  34. // gets plaintext from user
  35.  
  36. char *plain = get_string("plaintext: ");
  37. // prints modded cipher text
  38.  
  39. printf("ciphertext: ");
  40. for (int i = 0; n = strlen(plain), i < n; i++)
  41. {
  42. // checks case
  43. if (isupper(plain[i]))
  44. {
  45. int plainupper = (plain[i] - 65);
  46. int ciphupper = (plainupper + newkey) % 26;
  47. char fintext = (ciphupper + 65);
  48. printf("%c", fintext);
  49. }
  50. // checks case
  51. if (islower(plain[i]))
  52. {
  53. int plainlower = (plain[i] - 97);
  54. int ciphlower = (plainlower + newkey) % 26;
  55. char finntext = (ciphlower + 97);
  56. printf("%c", finntext);
  57. }
  58. else
  59. // prints whatever was typed if it isnt upper or lower case
  60. {
  61. printf("%c", plain[i]);
  62. }
  63.  
  64. }
  65. printf("\n");
  66. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement