Advertisement
Guest User

vigenere

a guest
Oct 20th, 2018
77
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.02 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <ctype.h>
  3. #include <stdlib.h>
  4. #include <cs50.h>
  5. #include <string.h>
  6.  
  7. int main(int argc, string argv[])
  8. {
  9. (void)argc;
  10. if (argv[1] != NULL)
  11. {
  12. string k = (argv[1]);
  13. string p = get_string("plaintext: \n");
  14. string c = p; //declaring "c" as a variable for the cipher
  15. printf("ciphertext: ");
  16. int j = 0;
  17. int m = strlen(k);
  18. for (int i = 0, n = strlen(p); i < n; i++)
  19. {
  20. if isalpha(p[i]) //checks if letter
  21. {
  22. if isupper(p[i]) //preserve case
  23. {
  24. if isupper(k[m]) //checks case of k[m]
  25. {
  26. c[i] = (char)((p[i] + k[m] - 130) % 26 + 65); //askii to alpha adjust to askii
  27. printf("%c", p[i]); //add character to string cipher
  28. }
  29. else //checks case of k[m]
  30. {
  31. c[i] = (char)((p[i] + k[m] - 162) % 26 + 65);
  32. printf("%c", p[i]);
  33. }
  34. }
  35. else //preserve case
  36. {
  37. if isupper(k[m])
  38. {
  39. c[i] = (char)((p[i] + k[m] - 162) % 26 + 97); //askii to alpha adjust to askii
  40. printf("%c", p[i]); //add character to string cipher
  41. }
  42. else
  43. {
  44. c[i] = (char)((p[i] + k[m] - 194) % 26 + 97);
  45. printf("%c", p[i]);
  46. }
  47. }
  48. j++; //counts to next character in key
  49. }
  50. else //prints non-letters
  51. {
  52. printf("%c", p[i]);
  53. }
  54. if (m == j) //checks if all characters in k have been used
  55. {
  56. j = 0; //reset j
  57. }
  58. }
  59. printf("\n");
  60. return 0;
  61. }
  62. return 1;
  63. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement