Go4Expert

Go4Expert (http://www.go4expert.com/)
-   C++ (http://www.go4expert.com/forums/cpp/)
-   -   C++ Caesar Cipher (http://www.go4expert.com/forums/cpp-caesar-cipher-t18313/)

dldsob 3Jul2009 05:22

C++ Caesar Cipher
 
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:

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;

         
  }


SaswatPadhi 4Jul2009 11:50

Re: C++ Caesar Cipher
 
Quote:

Originally Posted by dldsob (Post 51163)
I don't know if i did it right or not but can anyone help me with this code?

Why not !!
Welcome to Go4Expert Forum :)

Quote:

Originally Posted by dldsob (Post 51163)
You must output:

HELLOWORLDYOUARECRAZY
mjqqtctwqietzfwjhwffe

Bad way of giving a test case. You should mention like this :

Quote:

Input Arguments:
0 5 HELLOWORLDYOUARECRAZY

Output:

Original Message:
HELLOWORLDYOUARECRAZY
New Message:
mjqqtctwqietzfwjhwffe

OK. I read your code and found some problems :

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
    // The new message (encrypted/decrypted is printed when the respective func is
    called, so this statement should be above the call :
    cout<<"New message: "<<endl;

    // Why is this cin ??
    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;
  }

I corrected the code for you, and the working version is :

Code: C

#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;
  }



All times are GMT +5.5. The time now is 22:19.