C++ Caesar Cipher

Discussion in 'C++' started by dldsob, Jul 3, 2009.

  1. dldsob

    dldsob New Member

    Joined:
    Jul 3, 2009
    Messages:
    3
    Likes Received:
    0
    Trophy Points:
    0
    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;
    
               
      }
     
    Last edited by a moderator: Jul 3, 2009
  2. SaswatPadhi

    SaswatPadhi ~ Б0ЯИ Τ0 С0δЭ ~

    Joined:
    May 5, 2009
    Messages:
    1,342
    Likes Received:
    55
    Trophy Points:
    0
    Occupation:
    STUDENT !
    Location:
    Orissa, INDIA
    Home Page:
    http://www.crackingforfun.blogspot.com
    Why not !!
    Welcome to Go4Expert Forum :)

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

    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
        [COLOR=Red]// The new message (encrypted/decrypted is printed when the respective func is
        called, so this statement should be above the call :
    [/COLOR][COLOR=Red][B]     cout<<"New message: "<<endl;[/B][/COLOR]
    
        [COLOR=Red]// Why is this cin ?? [/COLOR]
    [B][COLOR=Red]     cin>>argv[3];[/COLOR][/B]
    
         //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:
    #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;
      }
     
    Last edited: Jul 4, 2009
    shabbir likes this.

Share This Page

  1. This site uses cookies to help personalise content, tailor your experience and to keep you logged in if you register.
    By continuing to use this site, you are consenting to our use of cookies.
    Dismiss Notice