Hi , I posted on the beginner's forum Earlier.
Even though it's a simple program , the assignment requires
the encryption to be done by an external function.
Basically the program receives an input , and encrypts it by
shifting the alphabetical letters by a desired amount. when the shift goes past Z
it should start over at the beginning of the alphabet.
Example : Encrypting "abcd" with a shift of 3 would output "defg"
my program returns a blank value.
1 2 3 4 5
|
Enter the shift distance: 3
Enter a string to be encrypted: abcd
The original string was: abcd
The encrypted message is:
Decryption of the encrypted message:
| |
Here is the main.cpp which we received pre-coded.
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
|
#include <iostream>
#include <limits>
#include <string>
#include "task2functions.h"
using namespace std;
int main()
{
int shift;
cout << "Enter the shift distance: ";
cin >> shift;
cin.ignore(std::numeric_limits<std::streamsize>::max(), '\n');
string testString;
cout << "Enter a string to be encrypted: ";
getline(cin,testString,'\n');
cout << "The original string was: " << testString << endl;
string encrypted;
encrypted = caesarCipher(testString, shift);
cout << "The encrypted message is: " << encrypted << endl;
cout << "Decryption of the encrypted message: ";
cout << caesarCipher(encrypted, -1 * shift) << endl;
return 0;
}
| |
The function.cpp , which is where I have to our programming
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
|
#include "task2functions.h"
#include <cstring>
#include <cctype>
#include <string>
using namespace std;
string caesarCipher(const string& text,int shift) // DO NOT CHANGE HEADER
{
int x;
string Encrypt;
for (x=0; x < text.length(); x++)
{
toupper(text[x]);
if (text[x] >= '!' && text[i] <= '@' || text[x] >= '[' && text[x] <= '}')
Encrypt[x] = text[x];
else if (text[x] + shift > 'Z')
Encrypt[x] = (text[x] -26) + shift;
else Encrypt[x] += shift;
}
return Encrypt ;
}
| |