-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcaesartest.c
More file actions
56 lines (49 loc) · 1.17 KB
/
caesartest.c
File metadata and controls
56 lines (49 loc) · 1.17 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
#include <stdio.h>
#include <string.h>
#include <ctype.h>
#include <stdlib.h>
int main(int argc, char *argv[])
{
if (argc != 2)
return 1;
char s[512];
printf("Please enter a string: ");
fgets(s, 512, stdin);
int sLen = strlen(s);
int key = atoi(argv[1]);
int overrun = 0;
for (int i = 0; i < sLen; i++)
{
char letter = s[i];
if (isalpha(letter))
{
if (isupper(letter))
{
if (letter + key > 'Z')
{
printf("%c", 'A' + (letter + key) % 'Z' - 1);
}
else // we didnt overrun
{
printf("%c", letter + key);
}
}
else if (islower(letter))
{
if (letter + key > 'z')
{
printf("%c", 'a' + (letter + key) % 'z' - 1);
}
else // we didnt overrun
{
printf("%c", letter + key);
}
}
}
if (!isalpha(letter))
{
printf("%c", letter);
}
}
return 0;
}