Caesar Cipher

This is a discussion on Caesar Cipher within the C++ Programming forums, part of the General Programming Boards category; Code: I don't know if i did it right or not but can anyone help me with this code? You ...

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

  2. #2
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,185
    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?

  3. #3
    Registered User
    Join Date
    Jul 2009
    Posts
    7
    this is what i m getting

  4. #4
    Registered User
    Join Date
    Jul 2009
    Posts
    7
    i dont know how to do this problem because i m new to the C++.

  5. #5
    Registered User
    Join Date
    Oct 2008
    Posts
    1,262
    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'.

  6. #6
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,048
    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, nort, etc.

  7. #7
    Registered User
    Join Date
    Oct 2008
    Posts
    1,262
    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.

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

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Another Caesar Cipher
    By Swordsman in forum C++ Programming
    Replies: 6
    Last Post: 09-07-2007, 08:56 AM
  2. About aes
    By gumit in forum C Programming
    Replies: 13
    Last Post: 10-24-2006, 03:42 PM
  3. caesar cipher help.
    By stormfront in forum C Programming
    Replies: 36
    Last Post: 11-22-2005, 07:45 PM
  4. Help with Caesar cipher
    By jcmichman in forum C++ Programming
    Replies: 1
    Last Post: 04-05-2005, 10:50 AM
  5. My little Caesar cipher emulator
    By dead_cell in forum C++ Programming
    Replies: 3
    Last Post: 01-16-2004, 12:05 AM

Tags for this Thread


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