Guest User

Untitled

a guest
Jul 20th, 2018
85
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.50 KB | None | 0 0
  1. #include <cs50.h>
  2. #include <stdio.h>
  3. #include <string.h>
  4. #include <stdlib.h>
  5. #include <ctype.h>
  6.  
  7. int main(int argc, string argv[])
  8. {
  9. //Make sure that there is only 1 correct argument
  10. if (argc != 2)
  11. {
  12. printf("Usage: ./caesar k\n");
  13. return 1;
  14. }
  15.  
  16. else
  17. {
  18. //Once I get the correct argument,k ,change the string into an int
  19. int k = atoi(argv[1]);
  20.  
  21. //Make sure the integer is non-negative
  22. if (k < 0)
  23. {
  24. printf("Usage: ./caesar k , k must be more than 0\n");
  25. return 1;
  26. }
  27.  
  28. else
  29. {
  30. //Prompt user for a code to encrypt
  31. string s = get_string("plaintext:");
  32. printf("ciphertext: ");
  33. for (int i = 0, n = strlen(s) ; i < n ; i++)
  34. {
  35. if (isalpha(s[i]))
  36. {
  37. //Encrypt Uppercase letter
  38. if (isupper(s[i]))
  39. {
  40. printf("%c", ((s[i] - 'A' + k) % 26) + 'A');
  41. }
  42. //Encrypt Lowercase Letter
  43. if (islower(s[i]))
  44. {
  45. printf("%c", ((s[i] - 'a' + k) % 26) + 'a');
  46. }
  47. }
  48. //Print back the text without changes
  49. else
  50. {
  51. printf("%c", s[i]);
  52. }
  53. }
  54. //Print a new line
  55. printf("\n");
  56. return 0;
  57. }
  58. }
  59. }
Add Comment
Please, Sign In to add comment