-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcaesar.c
More file actions
57 lines (56 loc) · 1.34 KB
/
caesar.c
File metadata and controls
57 lines (56 loc) · 1.34 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
57
#include <stdio.h>
#include <cs50.h>
#include <string.h>
#include <ctype.h>
#include <stdlib.h>
// taking values from command line
int main(int argc, string argv[])
{
int a;
// converting value from type string to int
if (argc != 2)
{
printf("Usage: ./caesar key\n");
return 1;
}
else if (strlen(argv[1]) >= 1)
{
a = atoi(argv[1]);
}
if (argc == 2 && a > 0)
{
// getting text from user
string text = get_string("plaintext: ");
printf("ciphertext: ");
for (int i = 0, s = strlen(text); i < s; i++)
{
int x1 = text[i] + a;
if (islower(text[i]) && text[i] + a > 'z')
{
do
{
x1 = x1 - 26;
}
while (x1 > 'z');
printf("%c", x1);
}
else if (isupper(text[i]) && text[i] + a > 'Z')
{
printf("%c", (text[i] - 26) + a);
}
else if ((text[i] >= 'a' && text[i] <= 'z') || (text[i] >= 'A' && text[i] <= 'Z'))
{
printf("%c", text[i] + a);
}
else
{
printf("%c", text[i]);
}
}
printf("\n");
}
else
{
printf("Usage: ./caesar key\n");
}
}