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;
}

Recommended Answers

All 5 Replies

I think you should send the argument "word" as a reference and not copies of the argument.

i actually worked at a place that used a Caesar Cipher to encrypt user passwords for the operators who ran the production equipment.

and the "encrypted" password file was stored locally on every production computer.

no joke.

my first day there, i was like.... "LOL, WUT?" :-O

I think you should send the argument "word" as a reference and not copies of the argument.

My friend... oh my friend
word is (as you see) an array, and the name "word" as a fixed pointer at the first item in the array ,so when you send it to the function , you do it like this.

func(int array[])
{
}
int array[50];
func(array);

----------------------

People please... I need help

>>I think you should send the argument "word" as a reference and not copies of the argument.
He is passing a pointer actually.

>>the encryption function is working fine, but I'm having troubles with the decryption.
Even Encryption is not good. Here is the output:

siddhant3s@Xion:~/Documents$ ./testofcallbyreference
Enter the word please:
mathematically
Please enter the key
5
Enter "e" to encrypt, "d" to decrypt
e
rf_mjrf_nhfqqd

So, what is the error?
I think you may have guessed.
Check if the Resultant character exceeds 'z' or 'Z' , If it does do the needfull

Look, I coded the function myself and it is working great;
I will give you the code of the encrypter to get you started, you should write the decrypter yourself:

void CaesarEncrypt(char word[],int key,int size)
{
    for (int i=0;i<size;i++)
    {
        if (word[i] >='A' && word[i] <='Z')
        {
            int OV=word[i] + key;//overflow

            word[i]=(OV <= 'Z')? OV :'A' + (OV-'Z'-1);
        }

        if (word[i]<='z'&&word[i]>='a')
        {
           int OV=word[i] + key;//overflow

            word[i]=(OV <= 'z')? OV :'a' + (OV-'z'-1);
        }

    }
}
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.