-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathProblema_8.c
48 lines (32 loc) · 885 Bytes
/
Problema_8.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
#include <stdio.h>
#include <string.h>
void desloca(char *texto, char *cifra, int shift);
char converte(char c, int shift);
int main() {
char str[100], cifra[100] = {0};
int i;
printf("Cifra? ");
fgets(str, 100, stdin);
/* remove '\n' e substitui por '\0' */
str[strlen(str) - 1] = '\0';
for (i = 1; i < 26; ++i) {
desloca(str, cifra, i);
printf("com shift +%2d: %s\n", i, cifra);
}
}
void desloca(char *texto, char *cifra, int shift) {
for (int i = 0; i < strlen(texto); ++i) {
cifra[i] = converte(texto[i], shift);
}
}
char converte(char c, int shift) {
int ascii = (int) c, decrypt;
if (ascii >= 97 && ascii <= 122) {
decrypt = (ascii + shift - 97) % 26;
if (decrypt < 0) {
return 123 + decrypt;
}
else return decrypt + 97;
}
else return c;
}