-
Notifications
You must be signed in to change notification settings - Fork 0
/
caesar.c
48 lines (36 loc) · 836 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
43
44
45
46
47
48
#include<stdio.h>
#include<ctype.h>
#include<string.h>
#include<stdlib.h>
#include<cs50.h>
int main(int argc,char* argv[]){
if(argc != 2){
printf("Error!\n");
return 1;
}
int keys = atoi(argv[1]);
char* plainText;
char cipherText[200];
int ascii;
char current;
int sum;
plainText = get_string("plaintext: ");
for(int x = 0,l = strlen(plainText); x < l; x++){
current = plainText[x];
if(isalpha(current)){
if(isupper((int)plainText[x])){
ascii = (int)current;
sum =(((ascii - 'A') + keys) % 26) + 'A';
cipherText[x] = (char)sum;
//sum =(((ascii - 'A') + keys) % 26) + 'A';
}
else{
ascii = (int)current;
cipherText[x] = (char)(((ascii - 'a') + keys) % 26) + 'a';
}
}
else cipherText[x] = plainText[x];
}
//printf("Sum: %c\n",sum);
printf("ciphertext: %s\n",cipherText);
}