Advertisement
Guest User

Untitled

a guest
Nov 13th, 2019
129
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.64 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 shift(char c);
  8.  
  9. int pass = 0;
  10. int d = 0;
  11. int main(int argc, string argv[])
  12. {
  13. if (argc !=2)
  14. {
  15. printf("Usage: .vigenere key\n");
  16. return 1;
  17. }
  18.  
  19. string key = argv[1];
  20. //non-ints should return non-0, so string should return 0
  21. if (atoi(key) != 0)
  22. {
  23. printf("Usage: .vigenere key\n");
  24. return 1;
  25. }
  26.  
  27. if (argc == 2)
  28. {
  29.  
  30. //iterates through the string length of arg 2
  31. for (int i = 0; i < strlen(argv[1]); i++)
  32. {
  33. //if characters in arg 2 are NOT digits, then pass gets 1 point
  34. if (isdigit(argv[1][i]) == 0)
  35. {
  36. //printf("%c\n", argv[1][i]);
  37. pass++;
  38. }
  39. //otherwise the program terminates
  40. else
  41. {
  42. printf("%c\n", argv[1][i]);
  43. printf("Usage: .vigenere keyword\n");
  44. return 1;
  45. }
  46. // printf("Pass: %i\n", pass);
  47. }
  48.  
  49. if (pass > 0)
  50. {
  51. string text = get_string("plaintext: ");
  52. printf("ciphertext: ");
  53.  
  54. for (int i = 0; i < strlen(text); i++)
  55. {
  56.  
  57. int c = 0;
  58. //printf("strlenKey: %lu\n", strlen(key));
  59. //printf("d: %i\n", d);
  60.  
  61. //char changed = text[i] + shift(key[i]);
  62. //printf("%c\n", changed);
  63. if (d >= strlen(key))
  64. {
  65. d = 0;
  66. }
  67.  
  68.  
  69. for (int j = 0; j <= 0; j++)
  70. {
  71. if (isalpha(text[i]) !=0 )
  72. {
  73. printf("%i\n", text[i]);
  74. printf("%i\n", shift(key[d]));
  75.  
  76. char code_char = shift(key[d]) + text[i];
  77. printf("%i\n", code_char);
  78. if (code_char > 122)
  79. {
  80. code_char = (code_char) - 26;
  81. }
  82. printf("%i\n", code_char);
  83. printf("%c", code_char);
  84. c++;
  85. d = c + d;
  86. }
  87.  
  88. else
  89. {
  90. char code_char = text[i];
  91. printf("%c", code_char);
  92. }
  93.  
  94. //printf("key: %c\n", key[j]);
  95. }
  96.  
  97.  
  98.  
  99.  
  100.  
  101. //printf("%i\n", d);
  102.  
  103. //printf("shift value: %i\n", shift(key[i]));
  104. }
  105. printf("\n");
  106. }
  107. //if pass is more than 0, which means all characters are digits, prints success
  108. if (pass == 0)
  109. {
  110. printf("Usage: .vigenere keyword\n");
  111. return 1;
  112. }
  113.  
  114. }
  115.  
  116.  
  117.  
  118. return 0;
  119. }
  120.  
  121. int shift(char c)
  122. {
  123. if (isalpha(c) != 0)
  124. {
  125. if (isupper(c) != 0)
  126. {
  127. int shift_by = c-65;
  128. return shift_by;
  129. }
  130. if (islower(c) != 0)
  131. {
  132. int shift_by = c-97;
  133. return shift_by;
  134. }
  135. }
  136. else
  137. {
  138. return 1;
  139. }
  140. return 0;
  141. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement