0

My program is almost working. The only error which occurs is at each iteration of every 3rd letter of the cipher (therefore something is not working with key[2]). I haven't been able to wrap my head around why this is occurring.

For example: when using key "BAZ", message "BARFOO" ciphers as "CAkGOh" instead of "CAQGON". There seems to be a discrepancy of 26 when using key[2]. k = (char) (((int) 'Q') + 26)

Any hint?

string message = GetString(); 
string key = argv[1];
int messageLength = strlen(message);
int countKey = 0;
int keyLength = strlen(argv[1]);
// From l. 75 to end of for loop, the secret is enciphered. 

for (int i = 0; i < messageLength; i++) {
    int j = (countKey % keyLength);

    if (!isalpha(message[i])){ // check whether message[i] is a non-alphabetic character
        printf("%c", message[i]);  // print anything that is not a letter as is
    }
    else {
        // Encipher lower case letters of message
        if (islower(message[i])){

            // Encipher this way if keyword character lowercase
            if (islower(key[j])){
                int cipher[i];
                cipher[i] = (((int)message[i] + ((int) key[j]) - 97));
                printf("%c", cipher[i]);
                }

            // Encipher this way if keyword character uppercase
            else {
                int cipher[i];
                cipher[i] = (((int)message[i] + ((int) key[j]) - 65)); 
                printf("%c", cipher[i]);
                } 
        }
        // Encipher upper case letters of message
        else{

            // Encipher this way if keyword character lowercase
            if (islower(key[j])){
                int cipher[i];
                cipher[i] = (((int)message[i] + ((int) key[j])- 97));
                printf("%c", cipher[i]);
                }

            // Encipher this way if keyword character uppercase
            else {
                int cipher[i];
                cipher[i] = (((int)message[i] + ((int) key[j]) - 65)); 
                printf("%c", cipher[i]);
                }             
        }                  
     countKey++;
     }
0

when you're doing

int cipher[i];

the variable cipher here is an array of size i. do you think you really need to use an array for this task?

also, as you may probably know, an array arr of size n has only valid indices from 0 through n - 1. accessing arr[n] in this case has an undefined behavior.

another point is that you're taking care of getting the current character in the keyword to be in [0, 25], but you're not taking care of getting the current character of the plain text to be in that range.

and lastly, I do see you handling the situation where the char may wrap around by taking the remainder of dividing by 26.

4
  • Oh! You're right. I don't actually need to specify an array for cipher! Through the layers of loops checking whether a plain text character is alpha and is lower case, I thought this would make sure the plain text character is in the range [0, 25]. Is this incorrect? How would the char wrap around? Again, I would have thought that the multiple loops would make sure this would not happen. Can you enlighten me? Thank you for your help, Kareem!
    – Chloe
    Nov 1 '15 at 0:15
  • @Chloe I'm afraid this isn't correct! uppercase and lowercase chars start at 65 and 97 respectively. so the char value 'C' for example maps to 67, but you need it to map to 2 because that's what the ciphering formula assumes. that is, a char maps to a value in [0, 25].
    – kzidane
    Nov 1 '15 at 8:23
  • @Chloe regarding the wrap-around, suppose the current plain char is 'x' which should map to 23 after doing the work explained in the answer as well as the comment above, and your key is 4. adding 4 to 23 gives us 27 which is out of bounds (since our range is [0, 25]). taking the remainder of dividing by 26 gives us the result that we expect (i.e., 1 which maps to b).
    – kzidane
    Nov 1 '15 at 8:29
  • Got it! Thanks again, Kareem!
    – Chloe
    Nov 1 '15 at 20:45

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .