Advertisement
yuntinghsu

vigenere0.c (x)

Dec 18th, 2017
80
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.42 KB | None | 0 0
  1. #include <cs50.h>
  2. #include <stdio.h>
  3. #include <string.h>
  4. #include <ctype.h>
  5. #include <stdlib.h>
  6.  
  7. int k_lenain(int argc, string argv[])
  8. {
  9. //should key in valid "keyword"
  10. if (argc == 2)
  11. {
  12. //get key
  13. string key = argv[1];
  14.  
  15.  
  16. int k_len = strlen(key);
  17. int j = 0;
  18. int key_index = 0;
  19.  
  20.  
  21.  
  22. if (key != NULL && isalpha(key[j]))
  23. {
  24. printf("plaintext: ");
  25. string p = get_string();
  26. printf("ciphertext: ");
  27.  
  28.  
  29. for (int i = 0, n = strlen(p); i < n; i++)
  30. {
  31. if (i == 0)
  32. {
  33. key_index = 0;
  34. }
  35. else if(p[i] != ' ')
  36. {
  37. if(key_index >= (k_len-1))
  38. {
  39. key_index = 0;
  40. }
  41. else
  42. {
  43. key_index++;
  44. }
  45. }
  46.  
  47. if (isalpha(p[i]) && isalpha(key[key_index]))
  48. {
  49.  
  50. if (isupper(p[i]) && isupper(key[key_index]))
  51. {
  52. char a = (((p[i]) - 65) + ((key[key_index]) - 65)) % 26;
  53. char b = a + 65;
  54. printf("%c", b);
  55.  
  56. }
  57.  
  58. if (isupper(p[i]) && islower(key[key_index]))
  59. {
  60. char a = (((p[i]) -65) + ((key[key_index]) - 97)) % 26;
  61. char b = a + 65;
  62. printf("%c", b);
  63.  
  64. }
  65.  
  66. if (islower(p[i]) && isupper(key[key_index]))
  67. {
  68. char a = (((p[i]) - 97) + ((key[key_index]) - 65)) % 26;
  69. char b = a + 97;
  70. printf("%c", b);
  71.  
  72. }
  73.  
  74. if (islower(p[i]) && islower(key[key_index]))
  75. {
  76. char a = (((p[i]) -97) + ((key[key_index]) - 97)) % 26;
  77. char b = a + 97;
  78. printf("%c", b);
  79.  
  80. }
  81. }
  82.  
  83. else
  84. {
  85. printf("%c", p[i]);
  86. }
  87.  
  88. }
  89. printf("\n");
  90. }
  91. return 1;
  92. }
  93. return 1;
  94. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement