-
Notifications
You must be signed in to change notification settings - Fork 55
/
Caesar.c
69 lines (60 loc) · 1.44 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
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
/*
* Caesar Cipher
*
* @author lellansin <lellansin@gmail.com>
* @website http://www.lellansin.com/tutorials/ciphers
*/
#include <stdio.h>
#include <ctype.h>
char getShiftCh(char ch, int shift, char start, char end);
/*
* 凯撒密码
*
* @param char * [in] words 要加密的字符串
* @param int [in] shift 位移
* @param char * [out] result 结果保存
*/
void caesar(char *words, int shift, char *result)
{
int i;
char *p = words;
for (i = 0; *p; p++, i++)
{
if (isalpha(words[i]))
{
if (islower(words[i]))
{
sprintf(result, "%s%c", result, getShiftCh(words[i], shift, 'a', 'z'));
} else
{
sprintf(result, "%s%c", result, getShiftCh(words[i], shift, 'A', 'Z'));
}
} else
{
sprintf(result, "%s%c", result, words[i]);
}
}
}
char getShiftCh(char ch, int shift, char start, char end)
{
int diff = ((ch - start) + shift) % 26;
if (diff >= 0) {
return start + diff;
}
else {
return end + diff + 1;
}
}
int main(int argc, char const *argv[])
{
char text[] = "hello, world!";
char cipherstext[1024], result[1024];
int shift = -5;
// 左移 5
caesar(text, shift, cipherstext);
printf("左移 %s\n", cipherstext);
// 右移 5
caesar(cipherstext, -shift, result);
printf("右移 %s\n", result);
return 0;
}