Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- #include <cstdio>
- #include <cstring>
- #include <cctype>
- using namespace std;
- const int alphabet = 26;
- int shift(int&);
- void encryption(int&, char[alphabet], char [30]);
- void decryption(int&, char[alphabet], char [30]);
- int main()
- {
- char value;
- char letters[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'};
- char input[30];
- int shiftamount;
- cout << "Which action do you want to perform?" << endl;
- cout << "(1) Encryption" << endl;
- cout << "(2) Decryption" << endl;
- while(value != 1 && value != 2)
- {
- cin >> value;
- switch(value)
- {
- case'1':
- {
- shift(shiftamount);
- encryption(shiftamount, letters, input);
- }
- case'2':
- {
- shift(shiftamount);
- decryption(shiftamount, letters,input);
- }
- default:
- cout << "Error: Wrong value chosen. Try again." << endl;
- }
- }
- return 0;
- }
- int shift(int& shiftamount)
- {
- cout << "How many letters to the right do you want the alphabet to shift?" << endl;
- cin >> shiftamount;
- return 0;
- }
- void encryption(int& shiftamount, char letters[], char input[])
- {
- cout << "Input the message you wish to encrypt:" << endl;
- cin.getline(input, 30);
- char cipher[30];
- for(int i = 0; i < alphabet; i++)
- {
- letters[i] = toupper(letters[i]);
- int x = input[i] - 'A';
- cipher[i] = x + shiftamount;
- cout << cipher[i];
- }
- return;
- }
- void decryption(int& shiftamount, char letters[], char input[])
- {
- cout << "Input the message you wish to decrypt:" << endl;
- cin.getline(input, 30);
- char cipher[30];
- for(int i = 0; i < alphabet; i++)
- {
- int x = input[i] - 'a';
- cipher[i] = x + shiftamount;
- cout << cipher[i];
- }
- return;
- }
Add Comment
Please, Sign In to add comment