Advertisement
Guest User

Untitled

a guest
Mar 23rd, 2019
75
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.64 KB | None | 0 0
  1. #include <cs50.h>
  2. #include <stdio.h>
  3. #include <string.h>
  4. #include <ctype.h>
  5. #include <stdlib.h>
  6.  
  7. int main(int argc, string argv[])
  8. {
  9. int key = 0;
  10. //Check that only one command line argument was entered, otherwhise prompt "Usage: ./caesar key"
  11. if (argc != 2)
  12. {
  13. printf("Usage: ./caesar key\n");
  14. return 1;
  15. }
  16. else
  17. {
  18. // loop through the command line arguments
  19. for (int i = 1; i < argc; i++)
  20. {
  21. // loop through the characters of the command line argument
  22. for (int j = 0, n = strlen(argv[i]); j < n; j++)
  23. {
  24. // check whether the given character is a number - if not, print "not a digit key"
  25. if (!isdigit(argv[i][j]))
  26. {
  27. printf("not a digit key\n");
  28. return 1;
  29. }
  30. }
  31. key = atoi(argv[i]);
  32. //printf("Success\n%i\n", key);
  33. }
  34. }
  35. string ptext = get_string("plaintext: ");
  36. printf("ciphertext: ");
  37. for (int k = 0, n = strlen(ptext); k < n; k++)
  38. {
  39. // check if plain text is lowercase and convert the characters
  40. if islower(ptext[k])
  41. {
  42. printf("%c", (((ptext[k] + key) - 97) % 26) + 97);
  43. }
  44. // check if plain text is uppercase and convert the characters
  45. else if isupper(ptext[k])
  46. {
  47. printf("%c", (((ptext[k] + key) - 65) % 26) + 65);
  48. }
  49. // for any ASCII value outside of a-z and A-Z just print the value without any conversions
  50. else
  51. {
  52. printf("%c", ptext[k]);
  53. }
  54. }
  55. printf("\n");
  56. return 0;
  57. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement