-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathcaesar.c
75 lines (66 loc) · 1.95 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
70
71
72
73
74
75
#include <stdio.h>
#include <cs50.h>
#include <string.h>
#include <ctype.h>
#include <stdlib.h>
int main(int argc, string argv[])
{
// No definite key is given or too many/less arguments
if (argc != 2)
{
printf("Usage: %s key\n", argv[0]);
return 1;
}
// Key is not an integer
int flag = 0;
for (int i = 0; argv[1][i] != '\0'; i++)
{
if (isdigit(argv[1][i]) == 0)
{
flag = 1;
break;
}
}
if (flag)
{
printf("Usage: %s key\n", argv[0]);
return 1;
}
// Valid key and arguments provided
printf("Success\n");
// Convert string key into an integer
int key = atoi(argv[1]);
// Get input from user
string plaintext = get_string("plaintext: ");
int length = strlen(plaintext);
char character;
// Print the cipher text using key for plain-text
printf("ciphertext: ");
for (int i = 0; i < length; i++)
{
character = plaintext[i];
if (isalpha(character))
{
// Convert letter to lowercase if applicable
// Then subtract its ascii value with 97 to get an absolute index in range(0 to 25) - [26 alphabets]
int alphabetPosition = tolower(character) - 97;
// Get the position of letter after rotating argv[1] times (Caesar’s algorithm)
alphabetPosition = (alphabetPosition + key) % 26;
// Upper cipher character for uppercase letter in plain-text
if (isupper(character))
{
alphabetPosition = alphabetPosition + 65;
character = (char)alphabetPosition;
}
// Lower cipher character for lowercase letter in plain-text
else
{
alphabetPosition = alphabetPosition + 97;
character = (char)alphabetPosition;
}
}
printf("%c", character);
}
printf("\n");
return 0;
}