Ceasar cipher and decipher question

thekevin07's Avatar, Join Date: Sep 2008
Go4Expert Member
I wrote Ceasar cipher and decipher functions and they seam to work fine but for some reason I'm not convinced they will work on my teachers computer what do you guys think?

The assignment was to ignore all chars that aren't a-z and crypt them with a key of 3 so a=d, b=e, etc. Then show the crypted txt in blocks of 5 chars

here are the functions
Code:
string decipher(string s)
{
	string decipher;
	decipher.resize(s.length());
	const char *str=s.c_str();
	for ( int j=0,i=0; str[i] ; i++ )
    {	
		//make sure string is a char between a-z
		//if not, increment j
		//j is used so no extra white space is built into decipher if char is not alphabetic
		if(str[i]>='a' && str[i]<='z')
		{
			if(str[i]<='c')
			{
				decipher[i-j] = ((str[i] - 2)-'a')+'z';
			}
			else
			{
				decipher[i-j] = str[i] - 3;
			}
			
		}
		else
		{
			j++;
		}
	}
	return decipher;
}
string cipher(string s)
{
	string cipher;
	cipher.resize(s.length());
	const char *str=s.c_str();
	for ( int j=0,i = 0 ; str[i] ; i++ )
    {
		//make sure string is a char between a-z
		//if not, increment j
		//j is used so no extra white space is built into cipher if char is not alphabetic
		if(str[i]>='a' && str[i]<='z')
		{
			if(str[i]>='x')
			{
				cipher[i-j] = ((str[i] + 2)-'z')+'a';
			}
			else
			{
				cipher[i-j] = str[i] + 3;
			}
			
		}
		else
		{
			j++;
		}
	}
	//add spaces every 5 blocks of chars
	int g=5;
	while(g<=cipher.length())
	{
		cipher.insert(g," ");
		g+=6;
	}
	
	return cipher;
}
Specificcaly the part that bothers me is

Code:
if(str[i]<='c')
{
	decipher[i-j] = ((str[i] - 2)-'a')+'z';
}
else
{
	decipher[i-j] = str[i] - 3;
}
is this type of manipulation standard and OK?

Last edited by thekevin07; 20Nov2008 at 10:58..
pradeep's Avatar, Join Date: Apr 2005
Team Leader
What compiler are you using? Did it compile for you?
thekevin07's Avatar, Join Date: Sep 2008
Go4Expert Member
Visual Studio 2008, teacher too our computers are the same. Yes it did compile and it works perfect but I don't understand why and I dont know if i am required to typecast as an int, and is the code stable?
freaklet's Avatar, Join Date: May 2012
Newbie Member
so I too, am having issues with a ceasar cipher assignment:
I have to set up a class that holds a string and calls member functions, 1 to print a private data string, 1 to encrypt the use inputted data, and another decrypt it here's some of the set up i have so far(i need to know if I'm in the ball park or need to rework the whole thing):
Code:
#include <iostream>
#include <cstdlib>
#include <string>

using namespace std;
/*******************************************************************************
* Function Name: main()
* Parameters: None
* Return Value: int
* Purpose: Create user Cipher
*******************************************************************************/
class Cipher{
	private:
		char key[];
		int key(int a = 0, a = ciphershift);
public:
	int getkey[];
	void encode(string str_en);//function member to encode
	void decode(string str_de); // function memeber to decode
	void ciphershift(int shift);//funtion member to input character shift
};

void Cipher::encode(string str_en){
	const int Max = 500;
	char str[Max];
	cout<< "Enter message to be endcoded: "<<endl;
	cin.get (str, Max);
	cout<< "You entered: "<< str << endl;
	return 0;
}

	void Cipher::decode(string str_de){
	cout << "Enter message to be decoded: "<<endl;
	getline (cin, str_de);
	return 0;
	}

void Cipher::ciphershift(int shift)
	cout<< "Enter number character shift (0-20): "<< endl;
	cin>> shift;

Last edited by shabbir; 8May2012 at 13:45.. Reason: Code blocks
pein87's Avatar
Ambitious contributor
How are you getting that to work without a dictionary of the alphabet?

Code: Cpp
char alphaUpper[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"};
char alphaLower[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"};

then loop through your text using a for loop and check the index position of the found character then switch it with the value three down. If the index is 26 start at position 0 and add 2 so you'd get C/c.