-
Notifications
You must be signed in to change notification settings - Fork 11
/
caesar.c
62 lines (61 loc) · 1.8 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
//A program that encrypts messages using Caesar’s cipher
//By getting an interger key in the same time with the execution of the program
//Prompt the user with plaintext and give a ciphertext
#include <cs50.h>
#include <stdio.h>
#include <stdlib.h>
#include <ctype.h>
#include <string.h>
int main(int argc, string argv[])
{
//checking if the user gave 2 arguments or not
if (argc != 2)
{
printf("Usage: ./caesar key\n");
return 1;
}
//this in case the other argument wasn't an inter or actually a letter i think if it can be done
//with the previous check it would be better program
int k = 0, x = strlen(argv[1]);
while (k < x)
{
if (isalpha(argv[1][k]))
{
printf("Usage: ./caesar key\n");
return 1;
}
k++;
}
string plaintext = get_string("Plaintext: ");
printf("ciphertext: ");
for (int i = 0, key = atoi(argv[1]), textlen = strlen(plaintext), answer; i < textlen; i++)
{
if (ispunct(plaintext[i]) || isspace(plaintext[i]))
{
printf("%c", plaintext[i]);
}
else if (islower(plaintext[i]) && plaintext[i] + key > 'z')
{
//decreasing the key by 26 the English alphabet in case the key was very large 65 for example :D
while ((plaintext[i] + key - 26) > 'z')
{
key -= 26;
}
printf("%c", plaintext[i] + key - 26);
}
else if (isupper(plaintext[i]) && plaintext[i] + key > 'Z')
{
while ((plaintext[i] + key - 26) > 'Z')
{
key -= 26;
}
printf("%c", plaintext[i] + key - 26);
}
else
{
printf("%c", plaintext[i] + key);
}
}
printf("\n");
return 0;
}