8 Replies - 3845 Views - Last Post: 18 October 2010 - 12:46 PM Rate Topic: *---- 2 Votes

#1 qwertzpoi  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 19
  • Joined: 13-October 10

Caesar Cipher

Posted 13 October 2010 - 10:59 AM

Hello all,
i need a help with this example:
input:

cipher // <-- action
AAB // <-- key
BLUE CAR // <-- text to be ciphered
example 2:
input:

decipher //<-- action
AAB // <-- key
CMVF DBS <--text to be deciphered
So far I have this
#include <iostream>
#include <string>
using namespace std;
void cipher();
void decipher();
int main()
{
    char action[20];
    char ok [] = "cipher";
    cin >> action;
if (strcmp(action, ok) == 0)
    {
              cipher();
    }
    else
    {
              decipher();
     }
     system("PAUSE");
     return 0;
}
void cipher()
{
    int i = 0;
    int offset;
  
	cout << "What do you want your password to be?\n";
	cin >> str1;
	cout << "What do you want your offset to be?\n";
	cin >> offset;
	while(i<strlen(str1)){
	   str1[i] = int(str1[i]) + offset;
	   i++;
	}
	
	cout << str1<<endl;

}
void decipher()
{
     cout << "decipher\n";
}


Explanation key: A= shift by 1 character, B shift by 2 characters and I also need to limit that it only cycles in 'A'-'Z' so after 'Z'->'A' and if the text is longer than the key it will continue again from the beginning of the key. Spaces stay intact.
The key has to be string and the text should not be because it can be bigger.
I know I have text in string here. it's because I don't know how to do the length operation with array.
I thought of converting the key into numbers and adding them to the text but I don't know how to write it. So any idea is appreciated. Thanks

Is This A Good Question/Topic? 0
  • +

Replies To: Caesar Cipher

#2 AmitTheInfinity  Icon User is offline

  • C Surfing ∞
  • member icon

Reputation: 109
  • View blog
  • Posts: 1,530
  • Joined: 25-January 07

Re: Caesar Cipher

Posted 13 October 2010 - 09:51 PM

I didn't get that KEY part in your examples. Caesar Cipher never has a key, a modified version of it would have varying offset from 1 to 26 instead of constant 3, but what's that key is for? even your example is not using that key (it used offset 1 for all characters).

Anyway, considering that str1 has the plain text you want to encrypt and offset is the number of characters you want to move forward the plain text there are few (can also be read as 'many') changes required in your code.

1. The string is containing the space as well so just adding offset is not ok. You have to first check if the character is an alphabet and only in that case add an offset to it.
2. You are right, the last characters from alphabets need to be rolled up and replaced with the first ones. Your strings showing only capital letters in string so I am assuming you will be entering only capital letters. In that case you have to keep a check that if the answer goes beyond 'Z' after adding offset then subtract 'Z' from the answer.

You can do exact opposite of this for decryption.
btw, always check out the snippet and tutorials section of this site before posting, such topics are already covered there.

I hope this will help you. :)
Was This Post Helpful? 1
  • +
  • -

#3 qwertzpoi  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 19
  • Joined: 13-October 10

Re: Caesar Cipher

Posted 14 October 2010 - 10:15 AM

Cheers for your reply. I have posted wrong example. The correct one is
INPUT
cipher
AAB
SIFRUJI SIFRU

OUTPUT
TJHSVLJ TKGSW

and vice versa for decipher
I came out with
converting the key string to ASCII values but I don't know how to combine it so that it would output the ciphered text.
#include <iostream>
#include <string>
using namespace std;
int toInt();
void sifrovat();
void desifrovat();
int main()
{
    char akce[20];
    char ok [] = "sifrovat";
    cin >> akce;
if (strcmp(akce, ok) == 0)
    {
              sifrovat();
    }
    else
    {
              desifrovat();
     }
     system("PAUSE");
     return 0;
}
void sifrovat()
{
    char str1[80];
    int i = 0,a;
    string offset;
  
	cout << "What do you want your password to be?\n";
	cin >> str1;
	cout << "What do you want your offset number to be?\n";
	toInt();
	while(i<strlen(str1)){
	   str1[i] = int(str1[i]) + (int)offset[a];
	   i++;
	}
	
	cout << str1<<endl;

}
void desifrovat()
{
     cout << "Desifrovat\n";
}
int toInt()
{
   string offset; //String
   int a; //Number of letters
   cout<<"Enter string : ";   /*Inputs the characters*/
   cin>>offset;
   for(a = 0; a!=offset.length(); ++a) /*Prints out each letter converted into a int value.*/
   return int(offset[a]);
}


the program of course crashes after entering the key because it is wrong. So my question is if you can point me how should I combine it. Thanks in advance
Was This Post Helpful? 0
  • +
  • -

#4 AmitTheInfinity  Icon User is offline

  • C Surfing ∞
  • member icon

Reputation: 109
  • View blog
  • Posts: 1,530
  • Joined: 25-January 07

Re: Caesar Cipher

Posted 15 October 2010 - 03:40 AM

Wrong... first, you didn't made the changes suggested in that while loop of yours so you won't be doing the correct conversion and second you are accepting the key (or offset whatever it is) incorrectly.

str1[i] = int(str1[i]) + (int)offset[a]; needed a change read my previous post properly and you will get what I am pointing at.

Then that toInt is totally wrong. What's happening inside is just accepting a string and returning first character from it. Then in main you don't even know which index of that offset array you are accessing (that a is not initialized).

Check the comments inside
#include <iostream>
#include <string>
using namespace std;
int toInt(); //change is to accept a char * parameter.
void sifrovat();
void desifrovat();
int main()
{
    char akce[20];
    char ok [] = "sifrovat";
    cin >> akce;
if (strcmp(akce, ok) == 0)
    {
              sifrovat();
    }
    else
    {
              desifrovat();
     }
     system("PAUSE");
     return 0;
}
void sifrovat()
{
    char str1[80];
    int i = 0,a;
    string offset;
  
	cout << "What do you want your password to be?\n";
	cin >> str1;
	cout << "What do you want your offset number to be?\n";
/* accept the value here itself. there is no use of accepting them inside function as you are not collecting it back in below lie.  Otherwise have a pointer in main and make it point to what toint() is returning. */
	toInt(); // send that string offset as parameter otherwise the while loop below it useless.
	while(i<strlen(str1)){
	   str1[i] = int(str1[i]) + (int)offset[a]; /*not going to work. you are not modifying a (it should traverse from 0 to string length of offset and keep rotating like that till your str1 finishes */
	   i++;
	}
	
	cout << str1<<endl;

}
void desifrovat()
{
     cout << "Desifrovat\n"; // what is this? you are supposed to have LOGIC of deciphering here.
}
int toInt()
{
   string offset; //String
   int a; //Number of letters
   cout<<"Enter string : ";   /*Inputs the characters*/
   cin>>offset;
/*What following for loop does? anyways, add a line inside this for loop which will subtract 'A' from every character of offset. This will automatically give you the number by which you want to shift your str1 characters. */
   for(a = 0; a!=offset.length(); ++a) /*Prints out each letter converted into a int value.*/
/*make sure you finish your for loop properly.  right now your return statement is inside for loop.*/
   return int(offset[a]); //What this function returns anyway? and even if it returned anything sensible, no one in main collects it!
}




Do come up with changes.

I hope this will help you. :)

--EDIT--
Added a few more comments.

This post has been edited by AmitTheInfinity: 15 October 2010 - 03:43 AM

Was This Post Helpful? 1
  • +
  • -

#5 qwertzpoi  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 19
  • Joined: 13-October 10

Re: Caesar Cipher

Posted 15 October 2010 - 08:56 AM

View PostAmitTheInfinity, on 15 October 2010 - 02:40 AM, said:

Wrong... first, you didn't made the changes suggested in that while loop of yours so you won't be doing the correct conversion and second you are accepting the key (or offset whatever

I don't know how to correct it. It seems too difficult for me, sorry that I bothered you with thing I don't know how to make.
The last thing I came up with is this code, I tried to do what you said but because I am stupid or not good enough to make it how you suggested
#include <iostream>
#include <string>
using namespace std;
int toInt(char offset);
void cipher();
void decipher();
int main()
{
    char akce[20];
    char ok [] = "cipher";
    cin >> akce;
if (strcmp(akce, ok) == 0)
    {
              cipher();
    }
    else
    {
              decipher();
     }
     system("PAUSE");
     return 0;
}
void cipher()
{
    char str1[80];
    int i = 0,a;
    string offset;
  
	cout << "What do you want your password to be?\n";
	cin >> str1;
	cout << "What do you want your offset number to be?\n";
	cin >> offset;
	toInt(offset); // send that string offset as parameter otherwise the while loop below it useless.
	while(i<strlen(str1)){
	   str1[i] = int(str1[i]) + strlen(offset);
	   i++;
	}
	
	cout << str1<<endl;

}
void decipher()
{
     cout << "Decipher\n";  // i don't know how to do the  cipher part so I left this empthy
}
int toInt(char offset)
{
    
   int a;
   for(a = 0; a!=strlen(offset); ++a)
   {
   offset[a] - 'A'; /* changed it, but I don't know how to return it back to main */
   }
}


In the attachment is the error.
http://img207.images...s/i/pict1n.jpg/

This post has been edited by qwertzpoi: 15 October 2010 - 08:58 AM

Was This Post Helpful? -1
  • +
  • -

#6 ishkabible  Icon User is offline

  • spelling expret
  • member icon





Reputation: 1618
  • View blog
  • Posts: 5,707
  • Joined: 03-August 09

Re: Caesar Cipher

Posted 15 October 2010 - 10:06 AM

a few things,
1) just post the error instead of using a picture, save me form going to a different page and saves your from the hassle of uploading a picture
2) you are making several type errors and i have no clue what line the errors are talking about.
3) i can see one of them, your passing a string to a function with a char argument, how is that supposed to work? what do you think that call is supposed to do?

This post has been edited by ishkabible: 15 October 2010 - 10:10 AM

Was This Post Helpful? 1
  • +
  • -

#7 janotte  Icon User is offline

  • code > sword
  • member icon

Reputation: 988
  • View blog
  • Posts: 5,135
  • Joined: 28-September 06

Re: Caesar Cipher

Posted 15 October 2010 - 08:09 PM

@qwertzpoi

ishkabible was giving you good advice about how to post and how to write better code. Giving a neg rep for that is not cool.

Stop using being dumb as an excuse to yourself. That's the path of the loser. You aren't a loser are you? I didn't think so!

You are quite smart enough to do this if you break the challenge down into small pieces and solve each puzzle one by one.

Stop, take a long deep breath.
Choose one problem you know you have.
Work on that problem. Ask us questions about that one problem. Focus on that one problem. Work on that one problem.
Once that is solved move to the next specific problem.
That's the way to get this done.

Now where do you want to start?

BTW - I too have no idea what that three letter "key" you keep showing us has to do with a Caesar Cypher.
Unless and until you can explain that non-standard thing to us it is going to be hard for anyone to help you get to the finish line. Caesar Cyphers are very common programming challenges but that idea of a three letter "key" is new to me so either this isn't a Caesar Cypher or it is a variant that you need to explain to us. However, that can wait till later if you want. Just choose one small problem and let's work on whatever that one is, it needn't be the "key" thing if you don't want it to be.
Was This Post Helpful? 1
  • +
  • -

#8 AmitTheInfinity  Icon User is offline

  • C Surfing ∞
  • member icon

Reputation: 109
  • View blog
  • Posts: 1,530
  • Joined: 25-January 07

Re: Caesar Cipher

Posted 18 October 2010 - 06:34 AM

Alright, let me assume that you have made some progress(!). At least 2-3 lines are different than your last code. So here are my updated comments in your code.

#include <iostream>
#include <string>
using namespace std;
int toInt(char offset); // You have a string offset variable and I told you to accept pointer or reference there so signature needs to have string& and parameter datatype.
void cipher();
void decipher();
int main()
{
    char akce[20];
    char ok [] = "cipher";
    cin >> akce;
if (strcmp(akce, ok) == 0)
    {
              cipher();
    }
    else
    {
              decipher();
     }
     system("PAUSE");
     return 0;
}
void cipher()
{
    char str1[80];
    int i = 0,a;
    string offset;
  
	cout << "What do you want your password to be?\n";
	cin >> str1;
	cout << "What do you want your offset number to be?\n";
	cin >> offset; // It's ok until you are accepting strings without whitespaces.  Think about will it accept something like "SIFRUJI SIFRU" ? 
	toInt(offset); 
	while(i<strlen(str1)){
	   str1[i] = int(str1[i]) + strlen(offset); //why are you adding strlen in str1? you are supposed to add offset not it's length.
/*Logic went totally wrong on above line.  You needed another index counter (like you have i for str1) for offset as it has offsets for every character.  And you were supposed to use it just as e.g. offset[a] for addition.*/
/* One more thing that I told you and you didn't changed is subtracting 'Z' from str[1] after above line if str[1] went above 'Z'. */
	   i++;
/* Here you were supposed to increment index counter for offset (e.g. 'a' that I used above for offset) by 1 and mod it by length of offset to keep it revolving.*/
	}
	
	cout << str1<<endl;

}
void decipher()
{
     cout << "Decipher\n";  // i don't know how to do the  cipher part so I left this empthy
}
int toInt(char offset) // you need to take string& here.
{
    
   int a;
   for(a = 0; a!=strlen(offset); ++a) //offset is a string object, you have a function in that class to get length.  Don't use strlen find that function.
   {
   offset[a] - 'A'; /* changed it, but I don't know how to return it back to main */
// You don't need to, if you use reference as I told at your function signature, it will do changes directly in offset you have in main.
   }
}






For information I am putting my understanding about this tweak of Caesar cipher.

There is a string say AMIT and there is a Key say AAB. so AAB defines how many characters to move forward, for each character in string. i.e. for above string AMIT, A will move by 1 (first A from Key), M will move by 1 (second A from Key), I will move by 2 (B from Key) then key rotates and T will move by 1 (again first A from key) and this will go on till the string finishes.

I hope this will help you. :)
Was This Post Helpful? 2
  • +
  • -

#9 qwertzpoi  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 19
  • Joined: 13-October 10

Re: Caesar Cipher

Posted 18 October 2010 - 12:46 PM

View PostAmitTheInfinity, on 18 October 2010 - 05:34 AM, said:

Alright, let me assume that you have made some progress(!). At least 2-3 lines are different than your last code. So here are my updated comments in your code.

#include <iostream>
#include <string>
using namespace std;
int toInt(char offset); // You have a string offset variable and I told you to accept pointer or reference there so signature needs to have string& and parameter datatype.
void cipher();
void decipher();
int main()
{
    char akce[20];
    char ok [] = "cipher";
    cin >> akce;
if (strcmp(akce, ok) == 0)
    {
              cipher();
    }
    else
    {
              decipher();
     }
     system("PAUSE");
     return 0;
}
void cipher()
{
    char str1[80];
    int i = 0,a;
    string offset;
  
	cout << "What do you want your password to be?\n";
	cin >> str1;
	cout << "What do you want your offset number to be?\n";
	cin >> offset; // It's ok until you are accepting strings without whitespaces.  Think about will it accept something like "SIFRUJI SIFRU" ? 
	toInt(offset); 
	while(i<strlen(str1)){
	   str1[i] = int(str1[i]) + strlen(offset); //why are you adding strlen in str1? you are supposed to add offset not it's length.
/*Logic went totally wrong on above line.  You needed another index counter (like you have i for str1) for offset as it has offsets for every character.  And you were supposed to use it just as e.g. offset[a] for addition.*/
/* One more thing that I told you and you didn't changed is subtracting 'Z' from str[1] after above line if str[1] went above 'Z'. */
	   i++;
/* Here you were supposed to increment index counter for offset (e.g. 'a' that I used above for offset) by 1 and mod it by length of offset to keep it revolving.*/
	}
	
	cout << str1<<endl;

}
void decipher()
{
     cout << "Decipher\n";  // i don't know how to do the  cipher part so I left this empthy
}
int toInt(char offset) // you need to take string& here.
{
    
   int a;
   for(a = 0; a!=strlen(offset); ++a) //offset is a string object, you have a function in that class to get length.  Don't use strlen find that function.
   {
   offset[a] - 'A'; /* changed it, but I don't know how to return it back to main */
// You don't need to, if you use reference as I told at your function signature, it will do changes directly in offset you have in main.
   }
}






For information I am putting my understanding about this tweak of Caesar cipher.

There is a string say AMIT and there is a Key say AAB. so AAB defines how many characters to move forward, for each character in string. i.e. for above string AMIT, A will move by 1 (first A from Key), M will move by 1 (second A from Key), I will move by 2 (B from Key) then key rotates and T will move by 1 (again first A from key) and this will go on till the string finishes.

I hope this will help you. :)

Thank you very much, your comments are like godsent.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1