Advertisement
Guest User

Untitled

a guest
Sep 18th, 2019
141
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.14 KB | None | 0 0
  1. #include <cs50.h>
  2. #include <stdio.h>
  3. #include <string.h>
  4. #include <ctype.h>
  5.  
  6. string get_input()
  7. {
  8. string plain = get_string("plaintext: ");
  9. return plain;
  10. }
  11.  
  12. int caesar_cipher(string s, int k)
  13. {
  14. printf("ciphertext: ");
  15. string cipher;
  16. int SIZE = strlen(s);
  17. for (int i = 0; i < SIZE; i++)
  18. {
  19. // Creates a place to store the ciphered value
  20. int c_key;
  21. if ( isupper(s[i]) )
  22. {
  23. c_key = ((int) s[i] - 'A' + k) % 26 + (int) 'A';
  24. } else if ( islower(s[i]) )
  25. {
  26. c_key = ((int) s[i] - 'a' + k) % 26 + (int) 'a';
  27. } else {
  28. c_key = (int) s[i];
  29. }
  30.  
  31. // Prints the (char) -> int s[i] + 1;
  32. printf("%c", (char) c_key);
  33. }
  34. printf("\n");
  35. return 0;
  36. }
  37.  
  38. int main(int argc, string argv[])
  39. {
  40. if ((argc > 1 && argc < 3))
  41. {
  42. // Stores key
  43. int k = atoi(argv[1]);
  44. // Gets plaintext
  45. string plain = get_input();
  46. // Cracks it up
  47. caesar_cipher(plain, k);
  48. } else
  49. {
  50. printf("Usage: ./caesar key\n");
  51. return 1;
  52. }
  53. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement