Advertisement
Guest User

Untitled

a guest
Nov 13th, 2019
114
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.35 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. }
  47.  
  48. if (pass > 0)
  49. {
  50. string text = get_string("plaintext: ");
  51. printf("ciphertext: ");
  52.  
  53. for (int i = 0; i < strlen(text); i++)
  54. {
  55.  
  56. int c = 0;
  57. //printf("strlenKey: %lu\n", strlen(key));
  58. //printf("d: %i\n", d);
  59.  
  60. //printf("%c\n", changed);
  61. if (d >= strlen(key))
  62. {
  63. d = 0;
  64. }
  65.  
  66. for (int j = 0; j <= 0; j++)
  67. {
  68. if (isalpha(text[i]) !=0 )
  69. {
  70. printf("%i\n", text[i]);
  71. printf("%i\n", shift(key[d]));
  72.  
  73. char code_char = shift(key[d]) + text[i];
  74. printf("%i\n", code_char);
  75. if (code_char > 122)
  76. {
  77. code_char = (code_char) - 26;
  78. }
  79. printf("%i\n", code_char);
  80. printf("%c", code_char);
  81. c++;
  82. d = c + d;
  83. }
  84.  
  85. else
  86. {
  87. char code_char = text[i];
  88. printf("%c", code_char);
  89. }
  90.  
  91. //printf("key: %c\n", key[j]);
  92. }
  93.  
  94.  
  95. }
  96. printf("\n");
  97. }
  98. //if pass is more than 0, which means all characters are digits, prints success
  99. if (pass == 0)
  100. {
  101. printf("Usage: .vigenere keyword\n");
  102. return 1;
  103. }
  104.  
  105. }
  106.  
  107.  
  108.  
  109. return 0;
  110. }
  111.  
  112. int shift(char c)
  113. {
  114. if (isalpha(c) != 0)
  115. {
  116. if (isupper(c) != 0)
  117. {
  118. int shift_by = c-65;
  119. return shift_by;
  120. }
  121. if (islower(c) != 0)
  122. {
  123. int shift_by = c-97;
  124. return shift_by;
  125. }
  126. }
  127. else
  128. {
  129. return 1;
  130. }
  131. return 0;
  132. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement