-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathshift.cpp
More file actions
69 lines (64 loc) · 2 KB
/
shift.cpp
File metadata and controls
69 lines (64 loc) · 2 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
#include <iostream>
#include <string>
using namespace std;
/*~~~~~~~~~~~ Declaring functions ~~~~~~~~ */
void encode();
void decode();
void menu() //Prompts user to input choices
{
int choice;
string opt[3] = {"1. Encode","2. Decode","3. Exit" };
cout<<opt[0]<<endl<<opt[1]<<endl<<opt[2]<<endl<<"Please enter your choice: ";
cin>>choice;
cin.ignore();
if(choice == 1) encode();
if(choice == 2) decode();
}
int main()
{
menu();
}
void encode() //Starts Encoding
{
string msg;
int key;
std::cout << "Please Enter your key: ";
std::cin >> key;
cin.ignore();
if(key < 2 || key > 25)
{
std::cout << "The Key must be in range 2-25" << '\n';
}
else
{
cout<<"Please Enter the plain text to Encrypt: ";
getline(cin,msg); //Takes plain text and saves it into msg
for(int i = 0; i < msg.size(); i++)
{
if(msg[i] >= char(65) && msg[i] <= char(77) || msg[i] >= char(97) && msg[i] <= char(109))
cout<< char(msg[i] + key);
else if(msg[i] > char(77) && msg[i] <= char(90) || msg[i] > char(109) && msg[i] <= char(122))
cout<< char(msg[i] - key);
else //for special characters
cout<<msg[i];
}
}
return;
}
void decode() //Starts Encoding
{
string msg;
int key = 3;
cout<<"Please Enter your Encrypted Text: ";
getline(cin,msg); //Takes plain text and saves it into msg
for(int i = 0; i < msg.size(); i++)
{
if(msg[i] >= char(65) && msg[i] <= char(77) || msg[i] >= char(97) && msg[i] <= char(109))
cout<< char(msg[i] - key);
else if(msg[i] > char(77) && msg[i] <= char(90) || msg[i] > char(109) && msg[i] <= char(122))
cout<< char(msg[i] - key);
else //for special characters
cout<<msg[i];
}
return;
}