fork(1) download
  1. #include <stdio.h>
  2. #include <inttypes.h>
  3. #include <ctype.h>
  4.  
  5. int calculateKey(char letter, int key, int start_key)
  6. {
  7. key = key == 0 ? start_key : (toupper(letter + 1) - 65) % key;
  8. key %= 26;
  9. return key;
  10. }
  11.  
  12. char cntelements(char* element_str)
  13. {
  14. int elements;
  15.  
  16. for(elements = 0; element_str[elements] != '\0'; ++elements)
  17. {
  18. elements = elements + 1;
  19. }
  20.  
  21. return elements;
  22. }
  23.  
  24. char encodeLetter(char letter, int key)
  25. {
  26. int shiftDirection = (key % 2 == 0) ? key : -key;
  27. int shifted = letter + shiftDirection;
  28. int letter_param;
  29.  
  30. if (letter > 96 && letter < 123)
  31. {
  32. if (shifted < 97)
  33. {
  34. letter_param = 122 - (97 - shifted) + 1;
  35. }
  36.  
  37. else if (shifted > 122)
  38. {
  39. letter_param = 97 + (shifted - 122) - 1;
  40. }
  41.  
  42. else
  43. {
  44. letter_param = shifted;
  45. }
  46. return letter_param;
  47. }
  48.  
  49. if (letter > 64 && letter < 91)
  50. {
  51. if (shifted < 65)
  52. {
  53. letter_param = 90 - (65 - shifted) + 1;
  54. }
  55.  
  56. else if (shifted > 90)
  57. {
  58. letter_param = 65 + (shifted - 90) - 1;
  59. }
  60.  
  61. else
  62. {
  63. letter_param = shifted;
  64. }
  65. return letter_param;
  66. }
  67.  
  68.  
  69. return letter;
  70. }
  71.  
  72. unsigned int cipher(char* cipher_str, int start_key)
  73. {
  74. int cntChars = 0;
  75. int elements = cntelements(cipher_str);
  76. int i;
  77. int key = 0;
  78.  
  79. for(i = 0; i <= elements; i++)
  80. {
  81.  
  82. if((cipher_str[i] < 91 && cipher_str[i] > 64) ||
  83. (cipher_str[i] < 123 && cipher_str[i] > 96))
  84. {
  85. cntChars++;
  86. }
  87. key = calculateKey(cipher_str[i], key, start_key);
  88. printf("%d\n", key);
  89. cipher_str[i] = encodeLetter(cipher_str[i], key);
  90. }
  91.  
  92. return cntChars;
  93. }
  94.  
  95. int main()
  96. {
  97. char word[] = "GGGG";
  98. cipher(word, 50);
  99. printf("%s", word);
  100. }
Success #stdin #stdout 0s 4352KB
stdin
Standard input is empty
stdout
24
7
0
24
-16
EZGE