Advertisement
Guest User

KANDEL910

a guest
Jul 21st, 2018
67
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.13 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <cs50.h>
  3. #include <string.h>
  4. #include <ctype.h>
  5.  
  6. int main(int argc, string argv[])
  7. {
  8.  
  9. string k = argv[1];
  10. //k will be the key
  11.  
  12. //this for loop checks if all of the characters are in the alphabet
  13. int l = strlen(k);
  14. for (int i = 0; i < l; i++)
  15. {
  16. if (isupper(k[i]) || islower(k[i]))
  17. {
  18. }
  19. else
  20. {
  21. printf ("error!\n");
  22. return 1;
  23. }
  24. }
  25. //this if checks if argc equals 2 which means the user has given a correct key
  26. if (argc != 2)
  27. {
  28. printf ("error!\n");
  29. return 1;
  30. }
  31.  
  32. string s = get_string("plaintext:");
  33.  
  34. for (int j = 0; j < strlen(s); j++)
  35. {
  36. int m = j % l;
  37. if (isupper(s[j]))
  38. {
  39. k[m] -= 65;
  40. s[j] += k[m];
  41. if (s[j] > 90)
  42. {
  43. s[j] -= 26;
  44. }
  45. }
  46. if (islower(s[j]))
  47. {
  48. k[m] -= 97;
  49. s[j] = s[j] + k[m] - 26;
  50. if (s[j] < 97)
  51. {
  52. s[j] += 26;
  53. }
  54. }
  55. }
  56. printf("ciphertext:%s\n", s);
  57. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement