C Program to Implement Caesar Cipher

«
»
This is a C Program to implement Caesar cipher. This is the simplest of all, where every character of the message is replaced by its next 3rd character.

Here is source code of the C Program to Implement Caesar Cypher. The C program is successfully compiled and run on a Linux system. The program output is also shown below.

  1. #include <stdio.h>
  2. #include <ctype.h>
  3.  
  4. #define MAXSIZE 1024
  5.  
  6. void encrypt(char*);
  7. void decrypt(char*);
  8.  
  9. int menu();
  10.  
  11. int main(void) {
  12.  
  13.     char c, choice[2], s[MAXSIZE];
  14.  
  15.     while (1) {
  16.         menu();
  17.  
  18.         gets(choice);
  19.  
  20.         if ((choice[0] == 'e') || (choice[0] == 'E')) {
  21.             puts("Input text to encrypt->");
  22.             gets(s);
  23.             encrypt(s);
  24.         } else if ((choice[0] == 'd') || (choice[0] == 'D')) {
  25.             puts("Input text to decrypt->");
  26.             gets(s);
  27.             decrypt(s);
  28.         } else
  29.             break;
  30.     }
  31.  
  32.     return 0;
  33. }
  34.  
  35. void encrypt(char*str) {
  36.     int n = 0;
  37.     char *p = str, q[MAXSIZE];
  38.  
  39.     while (*p) {
  40.         if (islower(*p)) {
  41.             if ((*p >= 'a') && (*p < 'x'))
  42.                 q[n] = toupper(*p + (char) 3);
  43.             else if (*p == 'x')
  44.                 q[n] = 'A';
  45.             else if (*p == 'y')
  46.                 q[n] = 'B';
  47.             else
  48.                 q[n] = 'C';
  49.         } else {
  50.             q[n] = *p;
  51.         }
  52.         n++;
  53.         p++;
  54.     }
  55.     q[n++] = '\0';
  56.     puts(q);
  57. }
  58.  
  59. void decrypt(char*str) {
  60.     int n = 0;
  61.     char *p = str, q[MAXSIZE];
  62.  
  63.     while (*p) {
  64.         if (isupper(*p)) {
  65.             if ((*p >= 'D') && (*p <= 'Z'))
  66.                 q[n] = tolower(*p - (char) 3);
  67.             else if (*p == 'A')
  68.                 q[n] = 'x';
  69.             else if (*p == 'B')
  70.                 q[n] = 'y';
  71.             else
  72.                 q[n] = 'z';
  73.         } else {
  74.             q[n] = *p;
  75.         }
  76.         n++;
  77.         p++;
  78.     }
  79.     q[n++] = '\0';
  80.     puts(q);
  81. }
  82.  
  83. int menu() {
  84.     puts("To encrypt, input e or E\n");
  85.     puts("To decrypt, input d or D\n");
  86.     puts("To exit, input any other letter\n");
  87.     puts("Your choice:->\n");
  88.     return 0;
  89. }

Output:

$ gcc CaesarCipher.c
$ ./a.out
 
To encrypt, input e or E
To decrypt, input d or D
To exit, input any other letter
Your choice:-> e
Input text to encrypt-> DharmendraHingu
 
DKDUPHQGUDHLQJX
 
To encrypt, input e or E
To decrypt, input d or D
To exit, input any other letter
Your choice:-> x

Sanfoundry Global Education & Learning Series – 1000 C Programs.

Note: Join free Sanfoundry classes at Telegram or Youtube
advertisement
advertisement

Here’s the list of Best Books in C Programming, Data Structures and Algorithms.

advertisement
advertisement
Subscribe to our Newsletters (Subject-wise). Participate in the Sanfoundry Certification contest to get free Certificate of Merit. Join our social networks below and stay updated with latest contests, videos, internships and jobs!

Youtube | Telegram | LinkedIn | Instagram | Facebook | Twitter | Pinterest
Manish Bhojasia - Founder & CTO at Sanfoundry
Manish Bhojasia, a technology veteran with 20+ years @ Cisco & Wipro, is Founder and CTO at Sanfoundry. He lives in Bangalore, and focuses on development of Linux Kernel, SAN Technologies, Advanced C, Data Structures & Alogrithms. Stay connected with him at LinkedIn.

Subscribe to his free Masterclasses at Youtube & technical discussions at Telegram SanfoundryClasses.