Skip to content

Commit 9cad32d

Browse files
committed
Fixing streak issue
1 parent 84e3190 commit 9cad32d

File tree

1 file changed

+90
-85
lines changed

1 file changed

+90
-85
lines changed
Lines changed: 90 additions & 85 deletions
Original file line numberDiff line numberDiff line change
@@ -1,85 +1,90 @@
1-
/*
2-
* Encrypts a plain text to decrypts an encrypted text back to plain using Caeser Cipher.
3-
* It is a type of substitution cipher in which each letter in the plaintext is replaced by a
4-
* letter some fixed number of positions down the alphabet. For example, with a left shift of 3,
5-
* D would be replaced by A, E would become B, and so on.
6-
* The method is named after Julius Caesar, who used it in his private correspondence
7-
* Enter plain text:the brown fox jumped over the lazy dog
8-
* Enter shift length:24
9-
* Encrypted text:rfc zpmul dmv hskncb mtcp rfc jyxw bme
10-
* Decrypted text:the brown fox jumped over the lazy dog
11-
*/
12-
13-
#include <iostream>
14-
#include <string>
15-
#include <sstream>
16-
17-
char caeser_encrypt(char c, int shift_length);
18-
char caeser_decrypt(char c, int shift_length);
19-
20-
int main()
21-
{
22-
std::string plain_text, encrypted_text;
23-
int shift_length;
24-
std::cout << "Enter plain text:";
25-
std::getline(std::cin, plain_text);
26-
std::cout << "Enter shift length:";
27-
std::cin >> shift_length;
28-
std::stringstream ss;
29-
30-
unsigned int text_length = plain_text.length();
31-
32-
// Encrypt the text
33-
//
34-
for (unsigned int i = 0; i < text_length; ++i)
35-
{
36-
ss << caeser_encrypt(plain_text[i], shift_length);
37-
}
38-
39-
encrypted_text = ss.str();
40-
std::cout << "Encrypted text:" << encrypted_text << std::endl;
41-
42-
// Reset the stream
43-
//
44-
ss.str(std::string());
45-
46-
// Decrypt the text again
47-
//
48-
for (unsigned int i = 0; i < text_length; ++i)
49-
{
50-
ss << caeser_decrypt(encrypted_text[i], shift_length);
51-
}
52-
53-
std::cout << "Decrypted text:" << ss.str() << std::endl;
54-
return 0;
55-
}
56-
57-
char caeser_encrypt(char c, int shift_length)
58-
{
59-
if (c >= 'A' && c <= 'Z')
60-
{
61-
return (((c - 'A' + shift_length) % 26) + 'A');
62-
}
63-
else if (c >= 'a' && c <= 'z')
64-
{
65-
return (((c - 'a' + shift_length) % 26) + 'a');
66-
}
67-
68-
// In case of non-alpha characters, return as it is.
69-
return c;
70-
}
71-
72-
char caeser_decrypt(char c, int shift_length)
73-
{
74-
if (c >= 'A' && c <= 'Z')
75-
{
76-
return (((c - 'A' + (26 - shift_length)) % 26) + 'A');
77-
}
78-
else if (c >= 'a' && c <= 'z')
79-
{
80-
return (((c - 'a' + (26 - shift_length)) % 26) + 'a');
81-
}
82-
83-
// In case of non-alpha characters, return as it is.
84-
return c;
85-
}
1+
/*
2+
* Encrypts a plain text to decrypts an encrypted text back to plain using Caeser Cipher.
3+
* It is a type of substitution cipher in which each letter in the plaintext is replaced by a
4+
* letter some fixed number of positions down the alphabet. For example, with a left shift of 3,
5+
* D would be replaced by A, E would become B, and so on.
6+
* The method is named after Julius Caesar, who used it in his private correspondence
7+
*
8+
* Enter plain text:the brown fox jumped over the lazy dog
9+
* Enter shift length:24
10+
* Encrypted text:rfc zpmul dmv hskncb mtcp rfc jyxw bme
11+
* Decrypted text:the brown fox jumped over the lazy dog.
12+
*/
13+
14+
#include <iostream>
15+
#include <string>
16+
#include <sstream>
17+
18+
char caeser_encrypt(char c, int shift_length);
19+
char caeser_decrypt(char c, int shift_length);
20+
21+
int main()
22+
{
23+
std::string plain_text, encrypted_text;
24+
int shift_length;
25+
std::cout << "Enter plain text:";
26+
std::getline(std::cin, plain_text);
27+
std::cout << "Enter shift length:";
28+
std::cin >> shift_length;
29+
std::stringstream ss;
30+
31+
unsigned int text_length = plain_text.length();
32+
33+
// Encrypt the text
34+
//
35+
for (unsigned int i = 0; i < text_length; ++i)
36+
{
37+
ss << caeser_encrypt(plain_text[i], shift_length);
38+
}
39+
40+
encrypted_text = ss.str();
41+
std::cout << "Encrypted text:" << encrypted_text << std::endl;
42+
43+
// Reset the stream
44+
//
45+
ss.str(std::string());
46+
47+
// Decrypt the text again
48+
//
49+
for (unsigned int i = 0; i < text_length; ++i)
50+
{
51+
ss << caeser_decrypt(encrypted_text[i], shift_length);
52+
}
53+
54+
std::cout << "Decrypted text:" << ss.str() << std::endl;
55+
return 0;
56+
}
57+
58+
// Encrypts a character based on shift length
59+
//
60+
char caeser_encrypt(char c, int shift_length)
61+
{
62+
if (c >= 'A' && c <= 'Z')
63+
{
64+
return (((c - 'A' + shift_length) % 26) + 'A');
65+
}
66+
else if (c >= 'a' && c <= 'z')
67+
{
68+
return (((c - 'a' + shift_length) % 26) + 'a');
69+
}
70+
71+
// In case of non-alpha characters, return as it is.
72+
return c;
73+
}
74+
75+
// Decrypts a character based on shift length
76+
//
77+
char caeser_decrypt(char c, int shift_length)
78+
{
79+
if (c >= 'A' && c <= 'Z')
80+
{
81+
return (((c - 'A' + (26 - shift_length)) % 26) + 'A');
82+
}
83+
else if (c >= 'a' && c <= 'z')
84+
{
85+
return (((c - 'a' + (26 - shift_length)) % 26) + 'a');
86+
}
87+
88+
// In case of non-alpha characters, return as it is.
89+
return c;
90+
}

0 commit comments

Comments
 (0)