caesar cipher code in c

Solutions on MaxInterview for caesar cipher code in c by the best coders in the world

showing results for - "caesar cipher code in c"
Amelie
15 Jan 2019
1#include <stdio.h>
2#include <ctype.h>
3
4#define MAXSIZE 1024
5
6void encrypt(char*);
7void decrypt(char*);
8
9int menu();
10
11int
12main(void)
13{
14
15char c,
16     choice[2],
17     s[MAXSIZE];
18
19 while(1)
20 {
21 menu();
22
23 gets(choice);
24
25 if((choice[0]=='e')||(choice[0]=='E'))
26 {
27  puts("Input text to encrypt->");
28  gets(s);
29  encrypt(s);
30 }
31 else if((choice[0]=='d')||(choice[0]=='D'))
32 {
33  puts("Input text to decrypt->");
34  gets(s);
35  decrypt(s);
36 }
37 else
38    break;
39 }
40
41 return 0;
42}
43
44void encrypt(char*str)
45{
46	int n=0;
47	char *p=str,
48		 q[MAXSIZE];
49
50	while(*p)
51	{
52	 if(islower(*p))
53	 {
54		 if((*p>='a')&&(*p<'x'))
55			 q[n]=toupper(*p + (char)3);
56		 else if(*p=='x')
57			 q[n]='A';
58		 else if(*p=='y')
59			 q[n]='B';
60		 else
61			 q[n]='C';
62	 }
63	 else
64	 {
65		 q[n]=*p;
66	 }
67	 n++; p++;
68	}
69	q[n++]='\0';
70	puts(q);
71}
72
73void decrypt(char*str)
74{
75	int   n=0;
76	char *p=str,
77		 q[MAXSIZE];
78
79	while(*p)
80	{
81	 if(isupper(*p))
82	 {
83		 if((*p>='D')&&(*p<='Z'))
84			 q[n]=tolower(*p - (char)3);
85		 else if(*p=='A')
86			 q[n]='x';
87		 else if(*p=='B')
88			 q[n]='y';
89		 else
90			 q[n]='z';
91	 }
92	 else
93	 {
94		 q[n]=*p;
95	 }
96	 n++; p++;
97	}
98	q[n++]='\0';
99	puts(q);
100}
101
102int menu()
103{
104 puts("To encrypt, input e or E\n");
105 puts("To decrypt, input d or D\n");
106 puts("To exit, input any other letter\n");
107 puts("Your choice:->\n");
108 return 0;
109}
Alma
09 May 2018
1#include <stdio.h>
2#include <string.h>
3#include <malloc.h>
4
5char *caesar(char t[], int l, int k, int a)
6{
7    char *ct = malloc(l);
8    for (int i = 0; i < l; i++)
9    {
10        int offset = (t[i] >= 65 && t[i] <= 90) ? 65 : 97;
11        int im = (a == 0) ? (t[i] + k) : (t[i] - k);
12        ct[i] = (t[i] == 32) ? 32 : ((im - offset) % 26) + offset;
13    }
14    ct[l] = '\0';
15    return ct;
16}
17
18int main()
19{
20    printf("Caesar Cipher");
21    int c = 0;
22    while (c != 3)
23    {
24        printf("\n\n1.Encrypt\n2.Decrypt\n3.Exit");
25        printf("\nSelect Option: ");
26        scanf("%d", &c);
27        switch (c)
28        {
29        case 1:
30        {
31            char pt[50];
32            int k;
33            printf("Enter plain text: ");
34            scanf(" %[^\n]%*c", pt);
35            printf("Enter the key: ");
36            scanf("%d", &k);
37            printf("\nCipher Text: %s", caesar(pt, strlen(pt), k, 0));
38            break;
39        }
40        case 2:
41        {
42            char ct[50];
43            int k;
44            printf("Enter cipher text: ");
45            scanf(" %[^\n]%*c", ct);
46            printf("Enter the key: ");
47            scanf("%d", &k);
48            printf("\nOriginal Text: %s", caesar(ct, strlen(ct), k, 1));
49            break;
50        }
51        case 3:
52            return 0;
53
54        default:
55            printf("\nInvalid Choice!");
56        }
57    }
58    return 0;
59}