-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcesar.c
More file actions
72 lines (61 loc) · 1.72 KB
/
cesar.c
File metadata and controls
72 lines (61 loc) · 1.72 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
//Begin with declarations
// Purpose: to use as a cesar cipher; string-> string
//int main(int ..., string argv) for design purposes is the template I created
//Important functions: GetString, atoi, strlen
//GetString gets a string, str len deals with the string length
//author: steierjo(Joshua Steier)
//5/13/2016
#include <stdio.h>
#include <cs50.h>
#include <string.h>
#include <ctype.h>
// declare the main with argv, array for strings
//need to read in string from the command line, so use int argc, string argv[]
int main(int argc, string argv[])
{
bool KeySuccessful = false;
//key is 26 as per the number of letters in the alphabet
int key = 26;
int length = 0;
string text = "";
do
{
// Must pass two commands in argc, so if not two, problem
if(argc != 2)
{
printf("We require an int as encryption key.\n");
return 1;
}
else
{
//use atoi
key = atoi(argv[1]);
KeySuccessful = true;
}
} while(!KeySuccessful);
// We can get the string this way
text = GetString();
//Use strlen
length = strlen(text);
for(int i = 0; i < length; i++)
{
if(isalpha(text[i]))
{
// account for lowercase text
if(islower(text[i]))
{
printf("%c", ((((text[i] - 97)+key)%26)+97));
}
else
{
printf("%c", ((((text[i] - 65)+key)%26)+65));
}
}
else
{
printf("%c", text[i]);
}
}
printf("\n");
return 0;
}