![]() |
| | #1 |
| Registered User Join Date: Jul 2009
Posts: 7
| Caesar Cipher Code:
I don't know if i did it right or not but can anyone help me with this code?
You must output:
HELLOWORLDYOUARECRAZY
mjqqtctwqietzfwjhwffe
THIS IS MY CODE:
#include <iostream>
#include <cstring>
#include <cctype>
using namespace std;
/*
* Description: This program operates on a command-line argument. It includes functions
* that implement a simple Caesar or Substitution cipher. Given key k and message m, the Caesar
* cipher will "shift" each letter in m k letters to the right using a standard english alphabet.
* The main program will take in a operation int, a key, and a message (either plaintext or
* encrypted. It will then output the message after it has been encrypted (operation 0) or
* decrypted (operation 1).
*/
// Function prototypes
void encrypt (int, char[]);
void decrypt (int, char[]);
char transform (char, int);
int main(int argc, char *argv[]) {
int n,key;
/*
* argv[0] is the name of the program
* argv[1] is the operation (encrypt 0 or decrypt 1)
* argv[2] is the key
* argv[3] is an array holding the message you want the program to operator on
*/
// First check to see if we received the correct number of arguments
// using argc. If not, print a "Usage" statement and return
if(argc != 4)
{
cout<<"Expected an integer and a string "<<endl;
return 0;
}
// Print original message supplied by user
cout<<"Print original message: "<<endl;
cout<<argv[3]<<endl;
// Convert the operation number and the numeric key to an integer
n=atoi(argv[1]);
key=atoi( argv[2]);
if(n==0)
encrypt(key,argv[3]);
else
decrypt(key,argv[3]);
// Call the appriopriate function based on the operation
// Print new message
cout<<"New message: "<<endl;
cin>>argv[3];
//system("PAUSE");
return 0;
}
/*
* Function name: transform
* Description: This function transforms (or shifts) <char> ch to another character
* <int> key letters away in the standard alphabet.
* Parameters:
* ch - a single character in the alphabet
* key - an integer that holds the number of shifts
* Return value: returns a "transformed" char
*/
char transform(char ch, int key)
{
// Array alpha contains the entire alphabet
char alpha[] = "abcdefghijklmnopqrstuvwxyz";
// Convert ch to lowercase so it can be matched to something in the alphabet
char low_case = tolower(ch);
// Loop until we locate the the letter or we reached until the end of the alphabet
int i=0;
while((low_case != alpha[i]) && (i < 26))
i++;
// Handle the occurance when the message contains a character not in the alphabet
if (i > 25)
return ch;
// Handle the case where the key could be larger than the number of characters in the alphabet
int new_key = key % 25;
// If we have a letter at the end of the alphabet and the key takes us off the end
// then we need start at the beginning
if (new_key + i > 25)
{
return alpha[(new_key + i) % 25];
}
else
{
return alpha[new_key + i];
}
}
/*
* Function name: encrypt
* Description: This function takes plain text <char> message and a <int> key and executes
* the Caesar ciphter on the entire message using the transform function.
* Parameters:
* key - the number of shifts to completed the cipher
* message - the message to be encrypted
* Return value: none
*/
void encrypt(int key, char mess[])
{int i=0;
while(mess[i]!='\0')
cout<<transform(mess[i++],key);
cout<<endl;
return;
}
/*
* Function name: decrypt
* Description: This function takes encrypted text <char> message and a <int> key and executes
* the Caesar ciphter on the entire message using the transform function.
* Parameters:
* key - the number of shifts to completed the cipher
* message - the message to be encrypted
* Return value: none
*/
void decrypt(int key, char mess[])
{int i=0;
while(mess[i]!='\0')
cout<<transform(mess[i++],-key);
cout<<endl;
return;
}
|
| dldsob is offline | |
| | #2 |
| and the Hat of Guessing Join Date: Nov 2007
Posts: 8,740
| There are 26 letters in the alphabet, so a shift of 27 spaces is the same as the shift of 1 space (not 2). If x < 25, then x%25 == x. Oh: and are you supposed to mimic the error in the example, or is that what you're getting? |
| tabstop is offline | |
| | #3 |
| Registered User Join Date: Jul 2009
Posts: 7
| this is what i m getting |
| dldsob is offline | |
| | #4 |
| Registered User Join Date: Jul 2009
Posts: 7
| i dont know how to do this problem because i m new to the C++. |
| dldsob is offline | |
| | #5 |
| Registered User Join Date: Oct 2008
Posts: 452
| You're making it very difficult for yourself like this, and it can be done a lot easier. Let's say you have a char. Remember that it is nothing but a number which, when displayed, will represent a character. Now, in ASCII (and probably every other character set; please correct me someone if I'm wrong) a through z are represented by sequential numbers. That is, 'b' == 'a' + 1, 'c' == 'b' + 1, etc. So if you have a char you can add 2 two get two characters further in the alphabet ('a' + 2 == 'c'). Same for subtraction. You can easily use this for this assignment. Although of course you'll have to make sure the result is not greater than 'z' or smaller than 'a'. |
| EVOEx is offline | |
| | #6 | |
| Frequently Quite Prolix Join Date: Apr 2005 Location: Canada
Posts: 7,629
| Quote:
There are some extremely obscure character sets like EBCDIC in which a-z are not contiguous. The standard doesn't specify that a-z must be contiguous, so if you want to write really portable programs it's best not to rely on this; but for nearly all computers you'd be safe in this assumption.For what it's worth: I suspect that the OP was given a set of functions to fill in, which means that they're stuck with this design. Forgive me if I'm wrong, but those don't seem like the sort of comments that most beginning programmers write.
__________________ dwk Seek and ye shall find. quaere et invenies. "Simplicity does not precede complexity, but follows it." -- Alan Perlis "Testing can only prove the presence of bugs, not their absence." -- Edsger Dijkstra "The only real mistake is the one from which we learn nothing." -- John Powell Other boards: DaniWeb, TPS Unofficial Wiki FAQ: cpwiki.sf.net My website: http://dwks.theprogrammingsite.com/ Projects: codeform, xuni, atlantis, etc. New project: nort | |
| dwks is offline | |
| | #7 | |
| Registered User Join Date: Oct 2008
Posts: 452
| Quote:
However, on a side note, at my university, we were allowed to assume ASCII. The program was fine when it compiled and ran on the Linux i386 computers at the labs. And whenever I code, I tend to assume ASCII or Unicode as well. I've never coded anything for other types of character sets, and I won't do so unless I know in advance that it might be required to run on other character sets. | |
| EVOEx is offline | |
| | #8 |
| Registered User Join Date: Jul 2009
Posts: 7
| Code: CAN ANYONE CHECK THIS CODE IF IT IS RIGHT OR WRONG?
#include <iostream>
#include <cstring>
#include <cctype>
using namespace std;
/*
* Description: This program operates on a command-line argument. It includes functions
* that implement a simple Caesar or Substitution cipher. Given key k and message m, the Caesar
* cipher will "shift" each letter in m k letters to the right using a standard english alphabet.
* The main program will take in a operation int, a key, and a message (either plaintext or
* encrypted. It will then output the message after it has been encrypted (operation 0) or
* decrypted (operation 1).
*/
// Function prototypes
void encrypt (int, char[]);
void decrypt (int, char[]);
char transform (char, int);
int main(int argc, char *argv[])
{
int n,key;
/*
* argv[0] is the name of the program
* argv[1] is the operation (encrypt 0 or decrypt 1)
* argv[2] is the key
* argv[3] is an array holding the message you want the program to operator on
*/
// First check to see if we received the correct number of arguments
// using argc. If not, print a "Usage" statement and return
if(argc != 4)
{
cout<<"Expected an integer and a string "<<endl;
return 0;
}
// Print original message supplied by user
cout<<"Print original message: "<<endl;
cout<<argv[3]<<endl;
cout<<"New message: "<<endl;
// Convert the operation number and the numeric key to an integer
n=atoi(argv[1]);
key=atoi( argv[2]);
if(n==0)
encrypt(key,argv[3]);
else
decrypt(key,argv[3]);
//system("PAUSE");
return 0;
}
/*
* Function name: transform
* Description: This function transforms (or shifts) <char> ch to another character
* <int> key letters away in the standard alphabet.
* Parameters:
* ch - a single character in the alphabet
* key - an integer that holds the number of shifts
* Return value: returns a "transformed" char
*/
char transform(char ch, int key)
{
// Array alpha contains the entire alphabet
char alpha[] = "abcdefghijklmnopqrstuvwxyz";
// Convert ch to lowercase so it can be matched to something in the alphabet
char low_case = tolower(ch);
// Loop until we locate the the letter or we reached until the end of the alphabet
int i=0;
while((low_case != alpha[i]) && (i < 26))
i++;
// Handle the occurance when the message contains a character not in the alphabet
if (i > 25)
return ch;
// Handle the case where the key could be larger than the number of characters in the alphabet
int new_key = key % 25;
// If we have a letter at the end of the alphabet and the key takes us off the end
// then we need start at the beginning
if (new_key + i > 25)
{
return alpha[(new_key + i) % 25];
}
else
{
return alpha[new_key + i];
}
}
/*
* Function name: encrypt
* Description: This function takes plain text <char> message and a <int> key and executes
* the Caesar ciphter on the entire message using the transform function.
* Parameters:
* key - the number of shifts to completed the cipher
* message - the message to be encrypted
* Return value: none
*/
void encrypt(int key, char mess[])
{
int i=0;
while(mess[i]!='\0')
cout<<transform(mess[i++],key);
cout<<endl;
return;
}
/*
* Function name: decrypt
* Description: This function takes encrypted text <char> message and a <int> key and executes
* the Caesar ciphter on the entire message using the transform function.
* Parameters:
* key - the number of shifts to completed the cipher
* message - the message to be encrypted
* Return value: none
*/
void decrypt(int key, char mess[])
{
int i=0;
while(mess[i]!='\0')
cout<<transform(mess[i++],-key);
cout<<endl;
return;
}
|
| dldsob is offline | |
![]() |
| Tags |
| bcz, heck, hemu, jack, whale |
| Thread Tools | |
| Display Modes | |
|
Similar Threads | ||||
| Thread | Thread Starter | Forum | Replies | Last Post |
| Another Caesar Cipher | Swordsman | C++ Programming | 6 | 09-07-2007 08:56 AM |
| About aes | gumit | C Programming | 13 | 10-24-2006 03:42 PM |
| caesar cipher help. | stormfront | C Programming | 36 | 11-22-2005 08:45 PM |
| Help with Caesar cipher | jcmichman | C++ Programming | 1 | 04-05-2005 10:50 AM |
| My little Caesar cipher emulator | dead_cell | C++ Programming | 3 | 01-16-2004 01:05 AM |