Advertisement
Guest User

Untitled

a guest
Mar 22nd, 2018
76
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 0.99 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <string.h>
  3. #define STR_LEN 50
  4. #define ASCII_LETTER_A 97
  5. #define LETTERS 26
  6. void decrypt(char* cipher, int n);
  7. int main(void)
  8. {
  9.     int key = 0;
  10.     char string[STR_LEN] = { 0 };
  11.  
  12.     printf("Enter cipher to decrypt: ");
  13.     fgets(string, STR_LEN, stdin);
  14.     string[strcspn(string, "\n")] = 0; //added a command that changes the fget's new line symbol into a char 0 ('0') Yup. That's something that we didn't learn! Thank you stackoverflow!
  15.  
  16.     printf("Enter decryption key: ");
  17.     scanf("%d", &key);
  18.     getchar();
  19.  
  20.     decrypt(string, key);
  21.  
  22.     printf("Decrypted text: ");
  23.     puts(string);
  24.  
  25.     getchar();
  26.     return 0;
  27. }
  28. void decrypt(char* cipher, int n)
  29. {
  30.     int i = 0, j = 0, lenStr = 0;
  31.     char newStr[STR_LEN] = { 0 };
  32.     lenStr = strlen(cipher);
  33.     for (i = lenStr - 1, j = 0; i >= 0; j++, i--)
  34.     {
  35.         *(newStr + j) = *(cipher + i);
  36.     }
  37.  
  38.     for (i = 0; i < strlen(newStr); i++)
  39.     {
  40.         *(cipher + i) = (*(newStr + i) + n - ASCII_LETTER_A) % LETTERS + ASCII_LETTER_A;
  41.         //puts(cipher);
  42.     }
  43. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement