Caesar Cipher Program in C
In this tutorial, we are going to see how to use the Caesar cipher to encrypt a message. Caesar’s cipher, also known as Shift Cipher, is one of the oldest and simplest forms of message encryption. This is a type of substitution cipher in which each letter of the original message is replaced by a letter corresponding to a number of letters shifted up or down in the alphabet.
Example Of Caesar Cipher
text = ABCD, Key = 10 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 Right shift by 10, A is replaced by K Right shift by 10, B is replaced by L Right shift by 10, C is replaced by M Right shift by 10, D is replaced by N
Output:
KLMN
Caesar Cipher Program in C
#include <stdio.h>
void cesar(char str[], int shift) {
int i = 0;
while (str[i] != '\0') {
if (str[i] >= 'A' && str[i]<= 'Z') {
char c = str[i] - 'A';
c += shift;
c = c % 26;
str[i] = c + 'A';
}
i++;
}
printf("%s", str);
}
int main()
{
char str[] = "ABCD";
cesar(str, 10);
return 0;
}
Output:
KLMN





