Hi, I'm trying to write a code for Caesar Cipher, I don't seem to be able to get it right, I'm having no compiling errors, but there're some real logical errors, the encryption function is working fine, but I'm having troubles with the decryption.
I'd love it if someone helps me, but please if you can make the decryption function work the same way the encryption one works, I mean "mathematically".
thx very much in advance
#include<iostream.h>
#include<string.h>
#include<windows.h>
void CaesarEncrypt(char word[],int key,int size);
void CaesarDecrypt(char word[],int key,int size);
void GetWord(char word[],int &size);
void PrintArray(char array[],int n);
int main()
{
int key,size=-1;
char word[100];
char choice;
cout << "Enter the word please:\n";
GetWord(word,size);
cout << "Please enter the key\n";
cin >>key;
cout << "Enter \"e\" to encrypt, \"d\" to decrypt\n";
cin>>choice;
switch (choice)
{
case 'E':case 'e':CaesarEncrypt(word,key,size);break;
case 'D':case 'd':CaesarDecrypt(word,key,size);break;
}
PrintArray(word,size);
return 0;
}
void CaesarEncrypt(char word[],int key,int size)
{
for (int i=0;i<size;i++)
{
if (word[i]>64&&word[i]<91)
{
word[i]=(char)(((word[i]+key-65)%26)+65);
}
if (word[i]<123&&word[i]>96)
{
word[i]=(char)(((word[i]+key-95)%26)+95);
}
}
}
void CaesarDecrypt(char word[],int key,int size)
{
for (int i=0;i<size;i++)
{
if (word[i]>64&&word[i]<91)
{
word[i]=(char)(((word[i]-key-65)%26)+65);
}
if (word[i]<123&&word[i]>96)
{
word[i]=(char)(((word[i]-key-95)%26)+95);
}
}
}
void GetWord(char word[],int &size)
{
do
{
size++;
word[size]=cin.get();//cin.get() was added by Maya Shallouf
}
while (word[size]!='\n');//'\n' was added by Maya Shallouf
}
void PrintArray(char array[],int n)
{
for (int i=0;i<n;i++)
{
cout << array[i];
}
cout <<endl;
}