C Board  

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

Reply
 
LinkBack Thread Tools Display Modes
Old 09-06-2007, 02:10 PM   #1
Apprentice
 
Swordsman's Avatar
 
Join Date: Apr 2007
Posts: 35
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?
Swordsman is offline   Reply With Quote
Old 09-06-2007, 03:40 PM   #2
and the hat of vanishing
 
Salem's Avatar
 
Join Date: Aug 2001
Location: The edge of the known universe
Posts: 21,214
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.
Up to 8Mb PlusNet broadband from only £5.99 a month!
Salem is offline   Reply With Quote
Old 09-07-2007, 07:36 AM   #3
Apprentice
 
Swordsman's Avatar
 
Join Date: Apr 2007
Posts: 35
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?
Swordsman is offline   Reply With Quote
Old 09-07-2007, 08:02 AM   #4
Kernel hacker
 
Join Date: Jul 2007
Location: Farncombe, Surrey, England
Posts: 15,686
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.
matsp is offline   Reply With Quote
Old 09-07-2007, 08:23 AM   #5
Apprentice
 
Swordsman's Avatar
 
Join Date: Apr 2007
Posts: 35
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
Swordsman is offline   Reply With Quote
Old 09-07-2007, 08:54 AM   #6
and the hat of vanishing
 
Salem's Avatar
 
Join Date: Aug 2001
Location: The edge of the known universe
Posts: 21,214
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.
Up to 8Mb PlusNet broadband from only £5.99 a month!
Salem is offline   Reply With Quote
Old 09-07-2007, 08:56 AM   #7
Kernel hacker
 
Join Date: Jul 2007
Location: Farncombe, Surrey, England
Posts: 15,686
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.
matsp is offline   Reply With Quote
Reply

Thread Tools
Display Modes

Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
Caesar Cipher dldsob C++ Programming 7 07-06-2009 06:06 PM
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 07:51 AM.


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