-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathProblema_7.c
50 lines (32 loc) · 887 Bytes
/
Problema_7.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
#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 shift;
printf("texto? ");
fgets(str, 100, stdin);
printf("shift? ");
scanf("%d", &shift);
/* Remove '\n' substituindo-o por '\0' */
str[strlen(str) - 1] = '\0';
desloca(str, cifra, shift);
printf("cifra: %s", 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, encrypt;
if (ascii >= 97 && ascii <= 122) {
encrypt = (ascii + shift - 97) % 26;
if (encrypt < 0) {
return 123 + encrypt;
}
else return encrypt + 97;
}
else return c;
}