Advertisement
Guest User

Untitled

a guest
Nov 18th, 2017
73
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.84 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <string.h>
  4. #include <ctype.h>
  5.  
  6. //26 letters in ASCII
  7. //cipher = 1st line. intercepted = 2nd line
  8.  
  9. //check if char is a letter
  10. int isletter(char *cipher, char *intercepted)
  11. {
  12. for (int i = 0; cipher[i] != '\0'; i++)
  13. {
  14. if (!isalpha(cipher[i]))
  15. {
  16. return 1;
  17. }
  18. }
  19. for (int i = 0; intercepted[i] != '\0'; i++)
  20. {
  21. if (!isalpha(intercepted[i]))
  22. {
  23. return 1;
  24. }
  25. }
  26. return 0;
  27. }
  28.  
  29. //compare length of cipher and intercepted
  30. int compare_length(char *cipher, char *intercepted)
  31. {
  32. for (int i = 0;; i++)
  33. {
  34. if (((cipher[i]) == '\0' && (intercepted[i]) != '\0') || ((intercepted[i]) == '\0' && (cipher[i]) != '\0'))
  35. {
  36. return 1;
  37. break;
  38. }
  39. else if (cipher[i] == '\0' && intercepted[i] == '\0')
  40. {
  41. return 0;
  42. break;
  43. }
  44. }
  45. }
  46.  
  47. int main(int argc, char *argv[])
  48. {
  49. //load the cipher
  50. char *cipher;
  51. cipher = (char *)malloc(1 * sizeof(char));
  52. int counter = 0;
  53. while (1)
  54. {
  55. scanf("%c", cipher + counter);
  56.  
  57. if (cipher[counter] == '\n')
  58. {
  59. cipher[counter] = '\0';
  60. break;
  61. }
  62. counter++;
  63. cipher = realloc(cipher, counter+1);
  64. }
  65.  
  66. //load the intercepted text
  67. char *intercepted;
  68. intercepted = (char *)malloc(1 * sizeof(char));
  69. counter = 0;
  70. while (1)
  71. {
  72. scanf("%c", intercepted + counter);
  73.  
  74. if (intercepted[counter] == '\n')
  75. {
  76. intercepted[counter] = '\0';
  77. break;
  78. }
  79. counter++;
  80. intercepted = realloc(intercepted, counter+1);
  81. }
  82.  
  83. //error messages
  84. if (isletter(cipher, intercepted) == 1)
  85. {
  86. fprintf(stderr, "Error: Chybny vstup!\n");
  87. free(cipher);
  88. free(intercepted);
  89. return 100;
  90. }
  91. else if (compare_length(cipher, intercepted) == 1)
  92. {
  93. fprintf(stderr, "Error: Chybna delka vstupu!\n");
  94. free(cipher);
  95. free(intercepted);
  96. return 101;
  97. }
  98.  
  99. //the thing for the Caesar cipher!
  100. int size_cipher = strlen(cipher)+1;
  101. counter = 0;
  102. char decrypted[size_cipher];
  103.  
  104. //shift in alphabet
  105. for (int i = 0; i <= 52; i++)
  106. {
  107. //compare cipher and intercepted text
  108. int temp_counter = 0;
  109. for (int j = 0; cipher[j] != '\0'; j++)
  110. {
  111. if (cipher[j] == intercepted[j])
  112. {
  113. temp_counter += 1;
  114. }
  115. }
  116. //getting new decrypted message
  117. if (temp_counter >= counter)
  118. {
  119. counter = temp_counter;
  120. strncpy(decrypted, cipher, size_cipher);
  121. }
  122. //shift the cipher
  123. for (int k = 0; cipher[k] != '\0'; k++)
  124. {
  125. cipher[k] = cipher[k] + 1;
  126. if (cipher[k] == 123)
  127. {
  128. cipher[k] = 'A';
  129. }
  130. else if (cipher[k] == 91)
  131. {
  132. cipher[k] = 'a';
  133. }
  134. }
  135. }
  136.  
  137. printf("%s\n", decrypted);
  138. //free the memory
  139. free(cipher);
  140. free(intercepted);
  141. return 0;
  142. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement