-
Notifications
You must be signed in to change notification settings - Fork 1
/
caesarShift.c
87 lines (66 loc) · 2.05 KB
/
caesarShift.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
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
/* Caesar shift */
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
/* Colors */
#define ANSI_COLOR_RED "\x1b[31m"
#define ANSI_COLOR_GREEN "\x1b[32m"
#define ANSI_COLOR_YELLOW "\x1b[33m"
#define ANSI_COLOR_BLUE "\x1b[34m"
#define ANSI_COLOR_MAGENTA "\x1b[35m"
#define ANSI_COLOR_CYAN "\x1b[36m"
#define ANSI_COLOR_RESET "\x1b[0m"
/* Shift letters */
#define JUMP 5
char *encrypt (char *string){
char encrypts[100] = {};
int i;
i = 0;
while(string[i] != '\0'){
if(toupper(string[i]) >= 65 || toupper(string[i]) <= 90){
if(toupper(string[i]+JUMP) > 90){
encrypts[i] = string[i]+JUMP-26;
}else if(string[i] == 32){
encrypts[i] = string[i];
}else{
encrypts[i] = string[i]+JUMP;
}
}
i++;
}
strcpy(string, encrypts);
return string;
}
char *decrypt(char *string){
char decrypts[100] = {};
int i;
i = 0;
while(string[i] != '\0'){
if(toupper(string[i]) >= 65 || toupper(string[i]) <= 90){
if(toupper(string[i]-JUMP) > 90){
decrypts[i] = string[i]-JUMP+26;
}else if(string[i] == 32){
decrypts[i] = string[i];
}else{
decrypts[i] = string[i]-JUMP;
}
}
i++;
}
strcpy(string, decrypts);
return string;
}
void menu(){
printf(ANSI_COLOR_BLUE "\t************************* \n" ANSI_COLOR_RESET);
printf(ANSI_COLOR_BLUE "\t W E L C O M E \n" ANSI_COLOR_RESET);
printf(ANSI_COLOR_BLUE "\t************************* \n" ANSI_COLOR_RESET);
printf(ANSI_COLOR_GREEN "\tInsert a word: " ANSI_COLOR_RESET);
}
int main(){
char string[100], *criptografa[100] = {}, cripAux[100] = {};
menu();
scanf("%[^\n]s", string);
strcpy(cripAux, encrypt(string));
printf(ANSI_COLOR_RED "\n\tString encrypted: " ANSI_COLOR_RESET "%s\n" , cripAux);
printf(ANSI_COLOR_BLUE "\tString decrypted: " ANSI_COLOR_RESET "%s\n", decrypt(cripAux));
}