C Board  

Go Back   C Board > General Programming Boards > C++ Programming

Reply
 
LinkBack Thread Tools Display Modes
Old 07-02-2009, 05:37 PM   #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   Reply With Quote
Old 07-02-2009, 05:43 PM   #2
and the Hat of Guessing
 
tabstop's Avatar
 
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   Reply With Quote
Old 07-02-2009, 05:54 PM   #3
Registered User
 
Join Date: Jul 2009
Posts: 7
this is what i m getting
dldsob is offline   Reply With Quote
Old 07-02-2009, 05:55 PM   #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   Reply With Quote
Old 07-03-2009, 05:42 AM   #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   Reply With Quote
Old 07-03-2009, 05:32 PM   #6
Frequently Quite Prolix
 
dwks's Avatar
 
Join Date: Apr 2005
Location: Canada
Posts: 7,629
Quote:
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.
Unfortunately, you're wrong. 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   Reply With Quote
Old 07-04-2009, 04:54 AM   #7
Registered User
 
Join Date: Oct 2008
Posts: 452
Quote:
Originally Posted by dwks View Post
Unfortunately, you're wrong. 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.
Hmmm you're right that it's likely functions he had to fill in.

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   Reply With Quote
Old 07-06-2009, 06:06 PM   #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   Reply With Quote
Reply

Tags
bcz, heck, hemu, jack, whale

Thread Tools
Display Modes

Forum Jump

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


All times are GMT -6. The time now is 05:47 PM.


Powered by vBulletin® Version 3.8.1
Copyright ©2000 - 2009, Jelsoft Enterprises Ltd.
Search Engine Optimization by vBSEO 3.3.0 RC2

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22