Advertisement
Guest User

Untitled

a guest
Nov 17th, 2019
90
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.42 KB | None | 0 0
  1. #include <cs50.h>
  2. #include <stdio.h>
  3. #include <string.h>
  4.  
  5. int key;
  6. int convert(string x);
  7. void digit(string y);
  8.  
  9. int main(int argc, string argv[])
  10. {
  11. printf("argv: %s\n", argv[1]);
  12.  
  13. //checks if positive digits
  14. digit(argv[1]);
  15.  
  16. if (argc != 2)
  17. {
  18. printf("Usage: ./caesar key");
  19. }
  20. else
  21. {
  22. key = convert(argv[1]);
  23. printf("Key: %i\n", key);
  24. string plain = get_string("Gimme plaintext: ");
  25. printf("plaintext: %s\n", plain);
  26. printf("ciphertext: ");
  27. for (int i = 0, len = strlen(plain) ; i < len; i++)
  28. {
  29. //QUESTION HERE!!!
  30. char cypher = (plain[i] + key) % 26;
  31. printf("%c", cypher);
  32. }
  33. printf("\n");
  34. }
  35.  
  36. }
  37.  
  38.  
  39. //is digit?
  40. void digit(string y)
  41. {
  42. int len = strlen(y);
  43. printf("Usage: ");
  44. for (int i = 0; i < len; i++)
  45. if (y[i] >= 48 && y[i] <= 57)
  46. {
  47. printf("%c", y[i]);
  48. }
  49. else
  50. {
  51. printf("./caesar key\n");
  52. exit(0);
  53. }
  54. printf("\n");
  55. }
  56.  
  57.  
  58. // convert string into digits, or my atoi
  59. int convert(string x)
  60. {
  61. int res = 0;
  62. int len = strlen(x);
  63.  
  64. printf("String: %s\n", x);
  65. printf("Len: %i\n", len);
  66.  
  67. for (int j = 0 ; j < len ; j++)
  68. {
  69. res = res * 10 + x[j] - '0';
  70. }
  71. return res;
  72. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement