-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcaesar.c
More file actions
48 lines (42 loc) · 1.42 KB
/
Copy pathcaesar.c
File metadata and controls
48 lines (42 loc) · 1.42 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
#include <stdio.h>
#include <cs50.h>
#include <stdlib.h>
#include <ctype.h>
#include <string.h>
int main(int argc, string argv[])
{
// check user input on command line
if (argc == 1)
{
printf("You did not enter a command line argument for the Caesar encryption key.\n");
printf("Please re-run the program and input one non-negative integer as a command line argument.\n");
return 1;
}
// check if the use entered more than one command line argument
else if (argc > 2)
{
printf("You entered too many command line arguments for the Caesar encryption key.\n");
printf("Please re-reun the program and input one non-negative integer as a command line argument.\n");
return 1;
}
else
{
//declare key as an int, assume that the user will only type integers at the prompt
int key = atoi(argv[1]);
string message = get_string("What is your message: ");
for (int i = 0, n = strlen(message); i < n; i++)
{
// add key to upper case letters
if (isupper(message[i]))
printf("%c", (message[i] + key));
// add key to lowercase letters
else if (islower(message[i]))
printf("%c", (message[i] + key));
// leave non-letters along
else
printf("%c", message[i]);
}
}
// Extra space at end
printf("\n");
}