Converting space character in string to integer

This is a discussion on Converting space character in string to integer within the C++ Programming forums, part of the General Programming Boards category; Hi all. I am currently trying to write a program that implements the Ceasar cypher, which shifts all the letters ...

  1. #1
    Registered User
    Join Date
    Oct 2006
    Posts
    4

    Converting space character in string to integer

    Hi all. I am currently trying to write a program that implements the Ceasar cypher, which shifts all the letters in a message a certain number of letters forward. The way I am doing this is turning each letter into its ASCII number via static_cast-ing, doing a numerical formula to apply the shift, and then changing the new numbers back into letters to get the encrypted letter.
    This worked fine for letters, but the problem is, I would like it to leave spaces (' ') alone, encrypting only letters themselves and leaving the spaces in the same spots. To do this, I say (where plaintext[i] is the original letter and cyphertext[i] is the new letter):

    if (plaintext[i] == ' ')
    cyphertext[i] = ' ';
    else
    //numerical formula for encrypting letters

    However, it is not working at all. It ends up converting spaces into letters or symbols. Is there some wierd issue that I am not aware of forbidding comparisons with blank space? Any suggestions for how to fix this would be greatly appreciated. Thanks.
    Last edited by boostage; 10-19-2006 at 08:50 PM.

  2. #2
    Registered User
    Join Date
    Oct 2006
    Location
    Canada
    Posts
    1,243
    does it work if you compare it to 32, its decimal value

  3. #3
    Registered User
    Join Date
    Oct 2006
    Posts
    4
    I already tried that, and it apparently never registered as being equal to 32 when I input spaces.

  4. #4
    Registered User
    Join Date
    Oct 2006
    Location
    Canada
    Posts
    1,243
    could you post as much relevant code (or all if not too much) for us to check out

  5. #5
    Registered User
    Join Date
    Oct 2006
    Posts
    4
    Code:
    for (int i = 0; i < len; ++i) {     //will cycle through all the letters of the message
            if (plaintext[i] == ' ') //ERROR HERE
                cyphertext[i] = ' ';
            else
                numtrans[i] = static_cast<int>(plaintext[i]);   //converts message letters to numbers
                numtrans[i] = ((numtrans[i] - 65 + keyshift) % 26) + 65;     //encyphers
                cyphertext[i] = static_cast<char>(numtrans[i]);     //converts back into letters, shifted
        }
    This is the piece of code I used to convert letters to numbers, encode them, then convert back. The only actual error is in the second line, where I am checking to see whether plaintext[i] is a blank space, but the rest of the code is included because it might be relevant. Note: len is the length of the string plaintext, and numtrans is an integer array to hold the numerical values of the letters.

    I tried replacing
    Code:
    if (plaintext[i] == ' ')
    with
    Code:
    if (static_cast<int>(plaintext[i]) == 32)
    but it did not help.

  6. #6
    Registered User
    Join Date
    Oct 2006
    Posts
    4
    Wait all, I am sorry, I found the error in my code. It was actually fairly trivial. I just left out some brackets after the "else" statement above, but since the space was the only thing acting up, I thought that was the error. My code is working fine now.

  7. #7
    Just Lurking Dave_Sinkula's Avatar
    Join Date
    Oct 2002
    Posts
    5,006
    For whatever reason, I was just writing a Caesar recently. FWIW as something to compare with:
    Code:
    #include <ctype.h>
    
    /**
     * Encode text using a Caesar cipher. Only alphabetic characters are encoded,
     * and in their original case (upper or lower).
     *
     * @param  text   pointer to the text to encrypt
     * @param  shift  the number of characters to shift (must be less than 26)
     *
     * @return pointer to the encoded text
     *
     * @example
     * <pre>char text[] = "All work and no play makes Jack a dull boy.";
     * puts(text);            // "All work and no play makes Jack a dull boy."
     * puts(caesar(text, 3)); // "Doo zrun dqg qr sodb pdnhv Mdfn d gxoo erb."</pre>
     *
     * @note http://en.wikipedia.org/wiki/Caesar_cipher
     */
    char *caesar(char *text, size_t shift)
    {
        static const char alpha[] = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";
        char *start = text;
        size_t i, j;
        for ( i = 0; text[i]; ++i )
        {
            for ( j = 0; alpha[j]; ++j )
            {
                if ( toupper(text[i]) == alpha[j] )
                {
                    char encoded = alpha[(j + shift) % (sizeof alpha - 1)];
                    text[i] = islower(text[i]) ? tolower(encoded) : encoded;
                    break;
                }
            }
        }
        return start;
    }
    (C, of course.)
    7. It is easier to write an incorrect program than understand a correct one.
    40. There are two ways to write error-free programs; only the third one works.*

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Direct3D problem
    By cboard_member in forum Game Programming
    Replies: 10
    Last Post: 04-09-2006, 04:36 AM
  2. someone who is good at finding and fixing bugs?
    By elfjuice in forum C++ Programming
    Replies: 8
    Last Post: 06-07-2002, 04:59 PM
  3. Converting a string to an integer
    By Unregistered in forum C++ Programming
    Replies: 3
    Last Post: 08-31-2001, 11:01 PM

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