Advertisement
Guest User

Untitled

a guest
Nov 20th, 2019
95
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.20 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. // checks if command line input is 2 or less
  10. if (argc != 2)
  11. {
  12. printf("Usage: ./caesar key\n");
  13. printf("1");
  14. return 1;
  15. }
  16. else if (argc == 2)
  17. {
  18. // check key for validity (positive int)
  19. // convert argv to int
  20. const int KEY = atoi(argv[1]);
  21. // store boolean
  22. bool KeyValid = true;
  23. // store key len
  24. int len = strlen(argv[1]);
  25.  
  26. // Loop checking each digit
  27. for (int i = 0; i < len; i++)
  28. {
  29. // if isdigit (part of ctype.h) detects a non-digit turns bool to false
  30. if (isdigit(argv[1][i]) == false)
  31. {
  32. // if anything besides positive int is entered the program will print false and end
  33. KeyValid = false;
  34. printf("false\n");
  35. return 0;
  36. }
  37. }
  38. // if input is positive int, this condition will execute
  39. if (KeyValid == true)
  40. {
  41. // this will prompt user for input
  42. string plain = get_string("plaintext: ");
  43. // this determines the length of the user input
  44. int plainLength = strlen(plain);
  45. // print "ciphertext: " once before the iteration loop happens
  46. printf("ciphertext: ");
  47. // iterate i against the length of plain text entered, basically scanning every character
  48. for (int i = 0; i < plainLength; i++)
  49. {
  50. // turn plain[i] into a character variable, so it can be scanned individually and printed
  51. char p = plain[i];
  52. //int ci = (p + KEY) % 26;
  53. if (isalpha(p) == false)
  54. {
  55. printf("%c", plain[i]);
  56. }
  57. if (isupper(p) == true)
  58. {
  59. printf("%c", p + KEY);
  60. }
  61. else
  62. {
  63. printf("%c", p + KEY);
  64. }
  65. }
  66. printf("\n");
  67. }
  68. }
  69. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement