Skip to content

Commit 508dd98

Browse files
authored
Merge pull request #374 from ArchismwanChatterjee/main
Added cipher algo and address comparison of variables
2 parents bcefa33 + 9c389a2 commit 508dd98

File tree

3 files changed

+140
-0
lines changed

3 files changed

+140
-0
lines changed

c/ROT13_cipher.c

+37
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
/*
2+
WAP to read any string/message from the user irrespective of the case
3+
Decode the string by shifting each alphabet 13 places.
4+
If the alphabet is 'Z' then it would be 'g' but make it in a way so that it gives 'M' that means keep a note of the case
5+
This is ROT13 Cipher Algorithm
6+
*/
7+
8+
#include <stdio.h>
9+
#include <string.h>
10+
11+
void decode_rot13(char *str)
12+
{
13+
int i;
14+
for (i = 0; str[i] != '\0'; i++)
15+
{
16+
if (str[i] >= 'A' && str[i] <= 'Z')
17+
{
18+
str[i] = (((str[i] - 'A') + 13) % 26) + 'A';
19+
}
20+
else if (str[i] >= 'a' && str[i] <= 'z')
21+
{
22+
str[i] = (((str[i] - 'a') + 13) % 26) + 'a';
23+
}
24+
}
25+
}
26+
27+
int main()
28+
{
29+
char str[] = "CNFFJBEq";
30+
decode_rot13(str);
31+
printf("Decoded string: %s\n", str);
32+
return 0;
33+
}
34+
/*
35+
OUTPUT
36+
Decoded string: PASSWORD
37+
*/

c/address_comparison.c

+26
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
// WAP to compare addresses of 2 variables and display appropriate message
2+
3+
#include <stdio.h>
4+
5+
int main()
6+
{
7+
int x, *p;
8+
int y, *q;
9+
p = &x;
10+
q = &y;
11+
printf("Address : %x \n", p);
12+
printf("Address : %x \n", q);
13+
if (p == q)
14+
printf("Equal");
15+
else
16+
printf("Not Equal");
17+
return 0;
18+
}
19+
/*
20+
OUTPUT
21+
22+
Address : 61ff14
23+
Address : 61ff10
24+
Not Equal
25+
26+
*/

c/caesar_cipher.c

+77
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,77 @@
1+
/*
2+
WAP to read any string/message from the user in uppercase
3+
Encrypt the string by shifting each alphabet x(<256) places.
4+
If the alphabet is 'Z' and x is 3 then it would be ']' but make it in a way so that it gives 'C'.
5+
This is CaesarCipher Algorithm
6+
*/
7+
8+
#include <stdio.h>
9+
#include <string.h>
10+
#include <math.h>
11+
#include <ctype.h>
12+
13+
int main()
14+
{
15+
16+
char ch[100], s[100];
17+
int x, j = 0;
18+
19+
printf("Type your code: \n");
20+
scanf("%s", &ch);
21+
22+
while (s[j]) // converting to uppercase
23+
{
24+
s[j] = toupper(ch[j]);
25+
j++;
26+
}
27+
28+
printf("Original code is \n");
29+
for (int i = 0; i < strlen(s); i++)
30+
{
31+
printf("%c", s[i]);
32+
}
33+
34+
printf("\n Enter any number < 256 \n");
35+
scanf("%d", &x);
36+
37+
printf("\n Encrypted code is \n");
38+
39+
if (x < 256)
40+
{
41+
for (int i = 0; i < strlen(s); i++)
42+
{
43+
int a = fabs((int)s[i] + x);
44+
45+
if (a > (int)'Z')
46+
{
47+
int d = (a - (int)'Z') % 26;
48+
if (d != 0)
49+
s[i] = (char)(64 + d);
50+
else
51+
s[i] = 'Z';
52+
}
53+
}
54+
55+
for (int i = 0; i < strlen(s); i++)
56+
{
57+
printf("%c", s[i]);
58+
}
59+
}
60+
else
61+
printf("Wrong input");
62+
return 0;
63+
}
64+
65+
/*
66+
OUTPUT:
67+
68+
Type your code:
69+
ABCD
70+
Original code is
71+
ABCD
72+
Enter any number < 256
73+
58
74+
Encrypted code is
75+
GHIJ
76+
77+
*/

0 commit comments

Comments
 (0)