-
Notifications
You must be signed in to change notification settings - Fork 16
/
Copy pathcaesar.c
53 lines (45 loc) · 1.06 KB
/
caesar.c
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
41
42
43
44
45
46
47
48
49
50
51
52
// CrypTools - GitHub
// Tuesday, 14 August 2018
// Caesar Cipher to encrypt text
/*****
Compile:
$ make
Use:
$ ./caesar KEYNUM < file.txt
*****/
#include <stdio.h>
#include <stdlib.h>
#define MINCAP 65
#define MAXCAP 90
#define MINLOW 97
#define MAXLOW 122
void encode (int shiftNumber, char letter);
// Takes an int and a file containing text to encrypt
int main (int argc, char *argv[]) {
int shift = 0;
int c = 0;
// set shift
shift = atoi (argv[1]);
while ((c = getchar()) != EOF) {
encode(shift, c);
}
printf("\n");
return EXIT_SUCCESS;
}
/* takes a character and increments it by shift */
void encode (int shift, char letter) {
int numAscii = letter;
int firstLetter;
if (numAscii >= MINCAP && numAscii <= MAXCAP) {
firstLetter = MINCAP;
} else if (numAscii >= MINLOW && numAscii <= MAXLOW) {
firstLetter = MINLOW;
} else {
putchar(letter);
return;
}
numAscii -= (firstLetter - shift);
numAscii = (numAscii % 26);
numAscii += firstLetter;
putchar(numAscii);
}