Advertisement
Guest User

Untitled

a guest
Mar 25th, 2017
67
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 0.79 KB | None | 0 0
  1. #include <stdlib.h>
  2. #include <stdio.h>
  3.  
  4. #define STR_LEN 50
  5.  
  6. void decrypt(char* cipher, int n);
  7. void strRev(char *str);
  8.  
  9. int main(void)
  10. {
  11.     char str[STR_LEN] = { 0 };
  12.     int decKey = 0;
  13.  
  14.     printf("Enter cipher to decrypt: ");
  15.     fgets(str, STR_LEN, stdin);
  16.     *(str + strlen(str) - 1) = 0;
  17.  
  18.     printf("Enter decryption key: ");
  19.     scanf("%d", &decKey);
  20.  
  21.     decrypt(str, decKey);
  22.     printf("Decrypted text: %s", str);
  23.  
  24.     system("PAUSE");
  25. }
  26.  
  27. void decrypt(char* cipher, int n)
  28. {
  29.     int i = 0, j = 0;
  30.  
  31.     strRev(cipher);
  32.  
  33.     for (; *cipher; cipher += 1)
  34.     {
  35.         *cipher = (((*cipher - 'a') + n) % 26) + 'a';
  36.     }
  37. }
  38.  
  39. void strRev(char *str)
  40. {
  41.     char tmp;
  42.     int i = 0, j = 0;
  43.     for (i = strlen(str) - 1, j = 0; i > j; i--, j++)
  44.     {
  45.         tmp = *(str + i);
  46.         *(str + i) = *(str + j);
  47.         *(str + j) = tmp;
  48.     }
  49. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement