-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcaesar_cipher.c
More file actions
39 lines (29 loc) · 787 Bytes
/
caesar_cipher.c
File metadata and controls
39 lines (29 loc) · 787 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
#include <stdio.h>
#include <ctype.h>
void encryptText(char *text, int key) {
for (int i = 0; text[i] != '\0'; i++) {
char ch = text[i];
// Only shift letters
if (isalpha(ch)) {
char base = isupper(ch) ? 'A' : 'a';
ch = (ch - base + key) % 26 + base;
}
text[i] = ch;
}
}
void decryptText(char *text, int key) {
encryptText(text, 26 - (key % 26)); // Reverse shift using same function
}
int main() {
char text[1000];
int key;
printf("Enter message: ");
fgets(text, sizeof(text), stdin);
printf("Enter shift key: ");
scanf("%d", &key);
encryptText(text, key);
printf("Encrypted: %s", text);
decryptText(text, key);
printf("Decrypted: %s", text);
return 0;
}