-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.c
More file actions
71 lines (55 loc) · 1.84 KB
/
main.c
File metadata and controls
71 lines (55 loc) · 1.84 KB
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
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
#include <stdio.h>
#include <ctype.h>
void perform_shift(int count, char *text, int mode)
{
// printf("TEXT: %s", text); //&s is for characters
// printf("MODE=%d\n", mode);
// printf("SHIFT=%d", count);
//null terminator
// v
for (int i = 0; text[i] != '\0'; i++) {
char c = text[i];
if (isalpha(c)) {
char base; // for upper / lower case detection
int shift = count;
if (mode == 1) {
shift = -count;
}
if (isupper(c)) {
base = 'A';
} else {
base = 'a';
}
int shifted = (c - base + shift) % 26; // not so secret formula from mr caesar
if (shifted < 0) {
shifted += 26;
}
text[i] = shifted + base;
}
}
printf("RESULT: %s\n", text);
}
int main() {
int mode; // 0 = encrypt, 1 = decrypt
int shift_count;
char text[1000]; // sets a fixed limit of 999 characters, will def replace with dynamic allocation later
do {
printf("\e[1;1H\e[2J"); // clear the console
printf("Select a mode (0 = encrypt, 1 = decrypt): ");
scanf("%d", &mode); //&d is for integers
while (getchar() != '\n'); // cclear leftover charracters frrom input buffer
} while (mode < 0 || mode > 1);
//printf("MODE=%d", mode);
do {
printf("Enter a shift number (1-25): ");
scanf("%d", &shift_count);
while (getchar() != '\n');
} while (shift_count < 1 || shift_count > 25);
printf("Enter some text: ");
fgets(text, 1000, stdin);
perform_shift(shift_count, text, mode);
// printf("TEXT: %s", buffer);
// printf("MODE=%d\n", mode);
// printf("SHIFT=%d", shift_count);
return 0;
}