![]() |
| | #1 |
| Apprentice Join Date: Apr 2007
Posts: 35
| Another Caesar Cipher 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;
}
|
| Swordsman is offline | |
| | #2 |
| and the hat of vanishing 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 | |
| | #3 |
| Apprentice 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' |
| Swordsman is offline | |
| | #4 |
| Kernel hacker Join Date: Jul 2007 Location: Farncombe, Surrey, England
Posts: 15,686
| Code: c = (isascii(c)):(((c-'A'+offset) % 26) + 'A':c; -- 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 | |
| | #5 |
| Apprentice Join Date: Apr 2007
Posts: 35
| |
| Swordsman is offline | |
| | #6 |
| and the hat of vanishing 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 | |
| | #7 | |
| Kernel hacker Join Date: Jul 2007 Location: Farncombe, Surrey, England
Posts: 15,686
| Quote:
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. } 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 | |
![]() |
| Thread Tools | |
| Display Modes | |
|
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 |