-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.cpp
More file actions
100 lines (90 loc) · 2.78 KB
/
main.cpp
File metadata and controls
100 lines (90 loc) · 2.78 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
//
// Bartlomiej Kunikowski
//
// Very simple caesar cipher decrypter and crypter program
//
// Copyright (c) 2020 BartekkPL
//
// This library is released under the Apache License 2.0.
// https://github.com/BartekkPL/caesar-cipher/blob/master/LICENSE
//
// https://github.com/BartekkPL/caesar-cipher
//
#include <iostream>
#include <limits>
#include <string>
void shiftCharacterInRange(int characterShift, char& characterToShift, char rangeBegin, char rangeEnd)
{
if (characterToShift >= rangeBegin && characterToShift <= rangeEnd)
{
if (characterToShift + characterShift <= rangeEnd && characterToShift + characterShift >= rangeBegin)
{
characterToShift += characterShift;
}
else if (characterToShift + characterShift > rangeEnd)
{
int remaindShift = characterToShift + characterShift - rangeEnd - 1;
characterToShift = rangeBegin + remaindShift;
}
else
{
int remaindShift = characterToShift + characterShift - rangeBegin + 1;
characterToShift = rangeEnd + remaindShift;
}
}
}
std::string CaesarCipher(const std::string& cipher, int chararcterShift)
{
std::string result = cipher;
chararcterShift = chararcterShift % ('z' - 'a');
auto shiftCharacter = [chararcterShift](char& ch)
{
if (ch >= 'a' && ch <= 'z')
{
shiftCharacterInRange(chararcterShift, ch, 'a', 'z');
}
else if (ch >= 'A' && ch <= 'Z')
{
shiftCharacterInRange(chararcterShift, ch, 'A', 'Z');
}
else
{
return;
}
};
for (char& ch : result)
{
shiftCharacter(ch);
}
return result;
}
int main()
{
std::cout << "CAESAR CIPHER (DE)CRYPTER" << std::endl << std::endl;
std::cout << "Enter Caesar cipher you want to (de)crypt: ";
std::string text;
std::getline(std::cin, text);
std::cout << std::endl << std::endl;
int currentShift = 0;
char chosenOption = '\0';
while (chosenOption != 'q' && chosenOption != 'Q')
{
std::cout << "Choose an option: " << std::endl;
std::cout << "- replace characters with \"bigger\" one - key \"d\"" << std::endl;
std::cout << "- replace characters with \"lower\" one - key \"a\"" << std::endl;
std::cout << "- exit program - key \"q\"" << std::endl;
std::cin >> chosenOption;
std::cin.ignore(std::numeric_limits<std::streamsize>::max(), '\n');
std::cout << std::endl << std::endl << std::endl;
if (chosenOption == 'd' || chosenOption == 'D')
++currentShift;
else if (chosenOption == 'a' || chosenOption == 'A')
--currentShift;
else
continue;
std::cout << "Original text: " << text << std::endl;
std::cout << "Modified one (actual shift: " << currentShift << "): " << CaesarCipher(text, currentShift);
std::cout << std::endl << std::endl << std::endl;
}
return 0;
}