-
Notifications
You must be signed in to change notification settings - Fork 0
/
caesar.c
42 lines (34 loc) · 881 Bytes
/
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
#include <stdlib.h>
#include <stdio.h>
#include <cs50.h> //provides GetString() to get a string from the user
#include <ctype.h>
#include <string.h>
//takes in char a, and increments it by k if it is a letter
char crypt(char a, int k);
int main(int argc, string argv[])
{
if (argc!=2)
{
printf("Invalid parameters.\n");
return 1;
}
int k = atoi(argv[1]);
string sentence = GetString();
for(int i=0, slength = strlen(sentence); i<slength; i++)
sentence[i] = crypt(sentence[i], k);
printf("%s\n", sentence);
}
char crypt(char a, int k)
{
int base; //base allows for the use of MOD on letter values of 0-25
if (isalpha(a))
{
if(islower(a))
base = 97;
else
base= 65;
return (a-base+k)%26+base;
}
else
return a;
}