-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCaesar done!
More file actions
81 lines (77 loc) · 1.96 KB
/
Caesar done!
File metadata and controls
81 lines (77 loc) · 1.96 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
69
70
71
72
73
74
75
76
77
78
79
80
81
#include <cs50.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <ctype.h>
int main(int argc, string argv[])
{
// Check if 2 commandline arguments are given, otherwise abort program
if (argc == 2)
{
// Loop through argv[1] to check if only numeric inputs are given
int n = strlen(argv[1]);
for (int i = 0; i < n; i++)
{
if (isdigit(argv[1][i]))
{
printf ("Success\n");
}
else
{
printf ("Usage: ./ceasar key\n");
return 1;
}
}
}
else
{
printf ("Usage: ./ceasar key\n");
return 1;
};
// Transform argv[1] to int and assign to key variable
int key = atoi(argv[1])%26;
printf ("%i\n", key);
// Ask user for secret message
string message = get_string("plaintext: ");
// Determine length of secret message
int length = strlen(message);
// Print "ciphertext: " for layout reasons
printf ("ciphertext: ");
// Loop through message to print every char misplaced by 1 on same line
for (int i = 0; i < length; i++)
{
// Retain uppercase
if (message[i] >= 65 && message[i] <= 90)
{
char newlet = message[i] + key;
if (newlet > 90)
{
printf ("%c", newlet - 26);
}
else
{
printf ("%c", newlet);
}
}
// Retain lowercase
else if (message[i] >= 97 && message[i] <= 122)
{
char newlet = message[i] + key;
if (newlet > 122)
{
printf ("%c", newlet - 26);
}
else
{
printf ("%c", newlet);
}
}
// Retain punctuation
else
{
printf ("%c", message[i]);
}
}
// Go to next line
printf ("\n");
}