/
main.c
71 lines (54 loc) · 1.85 KB
/
main.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
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
//
//
// Created by Wills Liou on 6/5/19.
//
//
/*
Title: Caesar Cipher
Description: This program takes in plain text and creates a cipher of that text.
How to use: Please enter plain type to encrypt: apples
The ciphered text is (with key shift: 1): bqqmft
Review: Suppose letter; a = 0, b = 1, c = 2, d = 3, e = 4
key = 1 (aka. key shift)
((int)letter + key) mod 26 ==> cast, return ciphered letter as number, cipherValue
==> (char)cipherValue = '[some letter]'
Demo: 'a' in apples --> a = (0 + 1) mod 26
How to run in terminal:
1) gcc main.c -o main // Compile
2) ./main // Run
Additional notes:
A, B, C, D, E, F, G, H, I, J, K, L, M, N, O, P, Q, R, S, T, U, V, W, X, Y, Z
a, b, c, d, e, f, g, h, i, j, k, l, m, n, o, p, q, r, s, t, u, v, w, x, y, z
ASCII 'a'= 97, 'b'= 98, 'c'= 99, 'd'= 100
*/
#include<stdio.h>// fget(), stdin macro
#include<string.h>// stringlen()
#include<stdlib.h>
// Define Caesar Cipher Function
void caesarCipher(char* plainText, int key);
int main(void){
int key = 1;
char plainText[101];
// Ask user
printf("Enter plain text to encrypt: ");
// Get input, store in plainText variable
fgets(plainText, sizeof(plainText), stdin);
// Print ciphered text
printf("The ciphered text is: ");
caesarCipher(plainText, key);
system("pause");
}
void caesarCipher(char* plainText, int key){
int i=0;
int cypherValue;
char cypher;
while(plainText[i] != '\0' && strlen(plainText)-1 > i) {// '\0' is enter char
cypherValue = ((int)plainText[i] - 97 + key) % 26 + 97; // ASCII - 'a'=97
cypher = (char)(cypherValue);
// Print characters
printf("%c", cypher);
i++;
}
// New line cuz OCD
printf("\n");
}