-
Notifications
You must be signed in to change notification settings - Fork 0
/
ceasers_ciasper.cpp
75 lines (57 loc) · 2.08 KB
/
ceasers_ciasper.cpp
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
//C++
#include <iostream>
#include <string>
std::string coder(int &coderType, std::string& entery, int& shift) {
std::string result = ""; // Holds the result after encoding/decoding
for (char c : entery) {
if (isalpha(c)) {
char base = islower(c) ? 'a' : 'A'; // Check if letter is lowercase or uppercase
if (coderType == 1) {
// Encoding: Shift forward
result += (c - base + shift) % 26 + base;
} else if (coderType == 2) {
// Decoding: Shift backward
result += (c - base - shift + 26) % 26 + base;
} else {
std::cout << "Invalid Input" << std::endl;
}
} else {
// Non-alphabetic characters remain unchanged
result += c;
}
}
return result; // Return the encoded/decoded result
}
int main() {
int coderType, shift;
std::string entery;
std::cout << "Please enter 1 to Encode and 2 to Decode: ";
std::cin >> coderType;
std::cout << "Please enter what to code: ";
std::cin >> entery;
std::cout << "Please enter how many shifts you want: ";
std::cin >> shift;
// Call the coder function and store the result
std::string result = coder(coderType, entery, shift);
// Output the result
std::cout << "The Coded value is: " << result << std::endl;
return 0;
}
//PYTHON
alphabet = ['a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n', 'o', 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y', 'z']
code = input("Type 1 to ENCODE and 2 to DECODE : ")
userInput = input("Write over here : ")
shiftSize = int(input("Enter shift number Password here : "))
coded = []
for char in userInput:
if char in alphabet:
index = alphabet.index(char)
if code == "1":
new_index = (index + shiftSize) % 26
elif code == "2":
new_index = (index - shiftSize) % 26
coded.append(alphabet[new_index])
else:
coded.append(char)
result = ''.join(coded)
print(f"The result is : {result}")