-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathceaser_cipher.c
More file actions
40 lines (33 loc) · 836 Bytes
/
ceaser_cipher.c
File metadata and controls
40 lines (33 loc) · 836 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
40
#include <stdio.h>
#include <string.h>
#include <ctype.h>
#define SIZE 110
void caser(int key, char PT[]) {
int i = 0;
char ch;
char CT[SIZE]; // better to use a fixed size array
while (PT[i] != '\0') {
ch = PT[i];
if (isalpha(ch)) {
if (islower(ch)) {
ch = ((ch - 'a' + key) % 26) + 'a';
} else if (isupper(ch)) {
ch = ((ch - 'A' + key) % 26) + 'A';
}
}
CT[i] = ch;
i++;
}
CT[i] = '\0';
printf("Cipher Text: %s\n", CT);
}
int main() {
int key;
char PT[SIZE];
printf("Enter plain text: ");
scanf("%[^\n]", PT); // read full line with spaces
printf("Enter the key value: ");
scanf("%d", &key);
caser(key, PT);
return 0;
}