-
Notifications
You must be signed in to change notification settings - Fork 0
/
caesar.c
43 lines (38 loc) · 902 Bytes
/
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
#include <cs50.h>
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <ctype.h>
bool check_key_validity(string s);
int main(int argc, string argv[])
{
if (argc != 2 || !check_key_validity(argv[1]))
{
printf("Usage: ./caesar key\n");
return 1;
}
int key = atoi(argv[1]);
string plaintext = get_string("plaintext: ");
printf("ciphertext: ");
for (int i = 0, len = strlen(plaintext); i < len; i++)
{
char c = plaintext[i];
if (isalpha(c))
{
char m = 'A';
if (islower(c))
m = 'a';
printf("%c", (c - m + key) % 26 + m);
}
else
printf("%c", c);
}
printf("\n");
}
bool check_key_validity(string s)
{
for (int i=0, len = strlen(s); i < len; i++)
if(!isdigit(s[i]))
return false;
return true;
}