Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <stdio.h>
- #include <string.h>
- #define STR_LEN 50
- #define ASCII_LETTER_A 97
- #define LETTERS 26
- void decrypt(char* cipher, int n);
- int main(void)
- {
- int key = 0;
- char string[STR_LEN] = { 0 };
- printf("Enter cipher to decrypt: ");
- fgets(string, STR_LEN, stdin);
- 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!
- printf("Enter decryption key: ");
- scanf("%d", &key);
- getchar();
- decrypt(string, key);
- printf("Decrypted text: ");
- puts(string);
- getchar();
- return 0;
- }
- void decrypt(char* cipher, int n)
- {
- int i = 0, j = 0, lenStr = 0;
- char newStr[STR_LEN] = { 0 };
- lenStr = strlen(cipher);
- for (i = lenStr - 1, j = 0; i >= 0; j++, i--)
- {
- *(newStr + j) = *(cipher + i);
- }
- for (i = 0; i < strlen(newStr); i++)
- {
- *(cipher + i) = (*(newStr + i) + n - ASCII_LETTER_A) % LETTERS + ASCII_LETTER_A;
- //puts(cipher);
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement