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
|
#include "CyclicShift.h"
#include<iostream>
#include<string>
using namespace std;
CyclicShift::CyclicShift()
{
char fUpperCase[26] = {'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'};//Initialization of class member
char fLowerCase[26] = {'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'};//Initialization of class member
}
string& CyclicShift::Encrypt(string& aOriginalMessage, int & aKey) const
{
int z;//z holds the value (int) of the length of the user input ( including spaces)
z = (int)aOriginalMessage.length(); /*give the variable z the value of the user input length and length is normally an unsigned long integer hence
we have to typecast it*/
/*counter that makes it keep looping until it "encrypts" all of the user input (that's why it keeps looping while its less than z)*/
for (int i = 0; i < z; i++)
{
for (int j = 0; j < 26; j++)
{
if (aOriginalMessage[i] == fLowerCase[j] || aOriginalMessage[i] == fUpperCase[j])
{
if (aKey > 0)
{
//another counter that loops forwards key times by incrementing method
for (int counter = 0; counter < aKey; counter++)
{
/*it checks if the letter text[x] is 'z' and if it is 'z' it will make it 'a'*/
if (aOriginalMessage[i] == 'z')
{
aOriginalMessage[i] = 'a';
}
else if (aOriginalMessage[i] == 'Z')
{
aOriginalMessage[i] = 'A';
}
else
{
aOriginalMessage[i]++;
}
}
}
else if (aKey < 0)
{
//another counter that loops backwards key times by decrementing method
for (int counter = 0; counter < abs(aKey); counter++)
{
/*it checks if the letter text[x] is 'a' and if it is 'a' it will make it 'z'*/
if (aOriginalMessage[i] == 'a')
{
aOriginalMessage[i] = 'z';
}
else if (aOriginalMessage[i] == 'A')
{
aOriginalMessage[i] = 'Z';
}
else
{
aOriginalMessage[i]--;
}
}
}
else
{
aOriginalMessage[i];//No alphabet shifts
}
}
else {
continue;
}
break;
}
}
return aOriginalMessage;
}
|