# Ceasar cipher and decipher question

Discussion in 'C++' started by thekevin07, Nov 20, 2008.

1. ### thekevin07New Member

Joined:
Sep 13, 2008
Messages:
29
0
Trophy Points:
0
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: Nov 20, 2008

Joined:
Apr 4, 2005
Messages:
1,645
87
Trophy Points:
0
Occupation:
Programmer
Location:
Kolkata, India
What compiler are you using? Did it compile for you?

3. ### thekevin07New Member

Joined:
Sep 13, 2008
Messages:
29
0
Trophy Points:
0
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?

4. ### freakletNew Member

Joined:
May 8, 2012
Messages:
1
0
Trophy Points:
0
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 a moderator: May 8, 2012
5. ### pein87Active Member

Joined:
Aug 6, 2010
Messages:
173
47
Trophy Points:
28
Occupation:
Web Dev
Location:
Limbo
How are you getting that to work without a dictionary of the alphabet?

Code:
```char alphaUpper = {"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 = {"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.