Thread: Another Caesar Cipher

  1. #1
    Apprentice Swordsman's Avatar
    Join Date
    Apr 2007
    Posts
    38

    Another Caesar Cipher

    Hello,

    I'm currently learning C++ and have decided to write a Caesar implementation. I have the code working but lack error checking (dealing with spaces, going back to start of alphabet) and am not sure how best to do it. Here's my method so far:

    Code:
    #include <iostream>
    #include <string>
    
    using namespace std;
    
    string sEncrypt(string& sInput, int iShift);
    
    int main(int argc, char *argv[])
    {
        // Declare variables
        string sInput; //text to encrypt, user inputted
        int iShift = 0; // shift value, user inputted
    
        
        cout << "Please enter the string to encrypt: ";
        getline(cin, sInput); //getline used to preserve spaces
        cout << "\n Please input the shift value: ";
        cin >> iShift;
        // pass values to Encryption functon
        sEncrypt(sInput, iShift);
        cout << "Encrypted String = " << sInput << endl;
        system("PAUSE");
        return EXIT_SUCCESS;
    }
    
    string sEncrypt(string& sInput, int iShift) //pass variable by reference
    {
        int iArrayValue = 0; //simple integer variable used to traverse the string array
        
           while(iArrayValue < sInput.length())
        {
                sInput[iArrayValue] = int(sInput[iArrayValue]) + iShift; //use ASCII values to change letter
                iArrayValue++;
        }
        return sInput;
    }
    I don't really want a bunch of if statements inside the while loop, but cannot think of any other way. Can somebody suggest something please?

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,656
    Use something like isalpha() to determine whether the character is a letter.
    As for wrap-around, look at modulo arithmetic.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  3. #3
    Apprentice Swordsman's Avatar
    Join Date
    Apr 2007
    Posts
    38
    I'm familiar with modular arithmetic already but I'm not sure how to implement a mod 26 function inside the code.

    As I'm using ASCII to shift the letters, could I just say something like:

    Code:
    Psuedo:
    
    if(letter value < 'A' or > 'Z')
    letter value = 'Z' or 'A'
    Obviously it would be two different statements. The problem that I'm having is that surely that would just assign the value of the new letter to be either A or Z, how would I make it 'wrap', is there a function or snippet that could help me please?

  4. #4
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Code:
    c = (isascii(c)):(((c-'A'+offset) % 26) + 'A':c;
    This assumes that all characters are upper-case, otherwise it'll get more complex.

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  5. #5
    Apprentice Swordsman's Avatar
    Join Date
    Apr 2007
    Posts
    38
    Quote Originally Posted by matsp View Post
    Code:
    c = (isascii(c)):(((c-'A'+offset) % 26) + 'A':c;
    This assumes that all characters are upper-case, otherwise it'll get more complex.

    --
    Mats
    Thanks, can you explain what this does though please as I'm a bit confused :P

  6. #6
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,656
    Well it should have been ? : to begin with ( not : : )

    Try writing it out in a longer form.
    c = c - 'A';
    c = c + offset;
    etc.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  7. #7
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Quote Originally Posted by Swordsman View Post
    Thanks, can you explain what this does though please as I'm a bit confused :P
    Ok, so to write it in a different way:
    Code:
    // c is a single char we need to convert - I'll leave it to you to "dig out the char, and store it back into the string"
    if (isalpha(c))   // Is it A..Z? // 0.
       c = (c - 'A') + offset;   // 1. This is a value offset..25+offset  (offset = A, 25+offset = Z). 
       c %= 26; // 2. This takes the remainder of division by 26, which is 0..25
       c += 'A';  // 3. Make it back into a letter.
    }
    Here's the values of the variable c at step 0, 1, 2 and 3 for some varying values of c, where offset = 3
    Code:
    0    1    2     3
    'A'  3    3    'D'
    'F'  8    8    'I'
    'W' 25   25    'Z'
    'Z' 28    2    'C'
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Caesar Cipher
    By dldsob in forum C++ Programming
    Replies: 7
    Last Post: 07-06-2009, 06:06 PM
  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, 08: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, 01:05 AM