forked from randerson112358/C-Programs
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcaesarCipher1.c
More file actions
56 lines (41 loc) · 1.58 KB
/
caesarCipher1.c
File metadata and controls
56 lines (41 loc) · 1.58 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
/*
Title: Caesar Cipher
Description: This program takes in a plain text and produces a cipher of that text using the Caesar Cipher
Usage: Please enter the plain text you want to encrypt: defend
The ciphered text is with (key shift = 1): efgfoe
How it work: Suppose Letter = a = 0, b=1, c=2, d=3, e=4
key = 1
((int)Letter + key) mod 26 ==> returns the ciphered letter as a number, cipherValue
==> (char)cipherValue = '[some letter]'
NOTE: ASCII 'a'=97, 'b'= 98, 'c'=99, 'd'=100, 'e'=101 ...
*/
#include<stdio.h>// fget(), stdin
#include<string.h>//stringlen()
#include<stdlib.h>
//Define my Caesar Cipher Function
void caesarCipher(char* plainText, int key);
int main(void){
int key = 1;
char plainText[101];
//Ask the user for the plain text to encrypt
printf("Please enter the plain text you want to encrypt: ");
//Get the users text input and store it in plainText variable
fgets(plainText, sizeof(plainText), stdin);
//Print the ciphered text
printf("The ciphered text is : ");
//Print the ciphered text
caesarCipher(plainText, key);
system("pause"); // Comment out if you are not using windows OS
}
void caesarCipher(char* plainText, int key){
int i=0;
int cypherValue;
char cypher;
while( plainText[i] != '\0' && strlen(plainText)-1 > i){
cypherValue = ((int)plainText[i] -97 + key) % 26 + 97;
cypher = (char)(cypherValue);
printf("%c", cypher);
i++;
}
printf("\n");
}