Guest User

Untitled

a guest
Jan 22nd, 2018
60
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.20 KB | None | 0 0
  1. #include <cs50.h>
  2. #include <stdio.h>
  3. #include <string.h>
  4. #include <stdlib.h>
  5. #include <ctype.h>
  6.  
  7. int main(int argc, string argv[])
  8. {
  9.  
  10. if (argc != 2)
  11. {
  12. printf("Usage: ./vigenere <keyword>\n");
  13. return 1;
  14. }
  15. int m = strlen(argv[1]);
  16.  
  17. for (int i = 0; i < m; i++)
  18. {
  19. if (isalpha( argv[1][i]) == false)
  20. {
  21. printf("Keyword must only contain letters A-Z and a-z\n");
  22. return 1;
  23. }
  24. }
  25.  
  26. printf("Plaintext: ");
  27. string text = get_string();
  28. for (int i = 0, j = 0, result = 0, n = strlen(text); i < n; i++)
  29. {
  30. char letter = text[i];
  31.  
  32. char key = argv[1][(j) % m];
  33.  
  34. if (isupper(key))
  35. {
  36. key -= 65;
  37. }
  38. else if (islower(key))
  39. {
  40. key -= 97;
  41. }
  42. if (isupper(letter))
  43. {
  44. result = (letter + key - 65) % 26 + 65;
  45. j++;
  46. }
  47. else if (islower(letter))
  48. {
  49. result = (letter + key - 97) % 26 + 97;
  50. j++;u
  51. }
  52.  
  53. else
  54. {
  55. result = letter;
  56. }
  57. printf("%c", result);
  58. }
  59. printf("\n");
  60. }
Add Comment
Please, Sign In to add comment