-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcaesar1.c
More file actions
68 lines (66 loc) · 1.64 KB
/
caesar1.c
File metadata and controls
68 lines (66 loc) · 1.64 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
58
59
60
61
62
63
64
65
66
67
68
#include <cs50.h>
#include <stdio.h>
#include <ctype.h>
#include <string.h>
#include <stdlib.h>
bool check_key(string s);
int main(int argc, string argv[])
{
//Check if there are 2 command-line arguments.
//Check for all chars to be digits.
string s = argv[1];
if (argc != 2 || check_key(s) == false)
{
printf("Usage: ./caesar key\n");
return 1;
}
//Convert the string into an integer.
int key = atoi(s);
//Prompt the user for planetext (The unencrypted text).
string pltxt = get_string("planetext: ");
printf("ciphertext: ");
int n = strlen(pltxt);
//Encypher the text using the key prompted while running the program.
for (int i = 0; i < n; i++)
{
char c = pltxt[i];
//Check is if is a letter.
if (isalpha(c))
{
//Check if is uppercase.
//Encypher.
if (isupper(c))
{
int ci = ((c - 'A' - key) % 26) + 'A';
printf("%c", ci);
}
//Check if is lowercase.
//Encrypt.
if (islower(c))
{
int cj = ((c - 'a' - key) % 26) + 'a';
printf("%c", cj);
}
}
//If isn't an alphabetical character, add to the cyphertext without modifications.
else
{
printf("%c", c);
}
}
printf("\n");
return 0;
}
//Function to check each char as digit.
bool check_key(string s)
{
for (int i = 0, n = strlen(s); i < n; i++)
{
char dg = s[i];
if (!isdigit(dg))
{
return false;
}
}
return true;
}