0

I'm currently working on pset2. I have the following problem however, which I cant seem to debug:

:( encrypts "BaRFoo" as "CaQGon" using "BaZ" as keyword \ expected output, but not "CaRGon\n" :( encrypts "BARFOO" as "CAQGON" using "BAZ" as keyword \ expected output, but not "CARGOO\n"

All other tests are passed by the cs50 check. This is the part of my code that carries out the encryption.

#define ALPHABET 26 

/*char *in: Text to be encrypted.
char *k: Keyword used for Vigenere encryption.
int sz1: Length of input string.
int sz2: Length of keyword. */

void VigCipher (char *in, char *k, int sz1, int sz2){

  int a = 0, b = 0, c = 0;

  for (a = 0; a < sz2; a++){

    if ( islower(k[a]) ){ //Set to 0-25
        k[a] -= 97;
    }        

    else if ( isupper(k[a]) ){ //Set to 0-25, such that uppercase and 
        k[a] -= 65;            //are equivalent.
    }
  }

  int wrap = 0;

  for (b = 0; b < sz1; b++){ // Up to string length of plaintext.

    if ( isalpha(in[b]) ){

        if (c == sz2) { //If c reaches strlen of key, bring back to 0.
            c = 0;
        }

        if (islower(in[b])) { // If dealing with lowercase
            if ( islower( in[b] + k[c]) ) { //Ensure it remains lowercase
                in[b] += k[c]; //Carry out
            }

            else { // If not, wrap around.
                wrap = ALPHABET - k[c];
                in[b] -= wrap;
            }
        }

        else if ( isupper( in[b] + k[c]) ) {
            if ( isupper( in[b] + k[c]) ) {
                in[b] += k[c];
            }

            else {
                wrap = ALPHABET - k[c];
                in[b] -= wrap;
            }
        }
        c++;
      }
    }
}

1 Answer 1

1

I see that your condition for checking the input to be upper-cased is wrong,

your condition is else if ( isupper( in[b] + k[c]) )

instead

it should be

else if ( isupper( in[b]))

because initially you have to check whether your input's 'b'th character is upper cased or not and then proceed to add the key and maintain its LetterCase

It seems when your program goes to the condition you placed and is checking if 'R' + 'Z' is upper-cased and hence skipping the wrap.

I hope this Help.

2
  • its skipping because 'R' + 'Z' doesn't result to be an upper-cased letter.
    – SAH20
    Commented Jul 20, 2016 at 14:00
  • Oh yeah, that was a dumb mistake. Thanks a lot mate Commented Jul 20, 2016 at 14:25

You must log in to answer this question.

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