0

When my cipher algo sees an "a" it seems to wipe out the rest of the keyword, returning only a " ", which seems to erase the rest of my keyword, leaving my encryption useless. I've debugged this several times and it works with other letters

Full code below:

#include <stdio.h>
#include <cs50.h>
#include <string.h>
#include <ctype.h>
// getting stuck passing letters into integers to create one cypher
int main(int argc, char *argv[]){
    if (argc == 2){
char *key_word = argv[1];
int length = strlen(key_word);
//convert to ciper values and store as a converted key
for (int k = 0; k < length; k++){
    if (islower(key_word[k])) {
        int num = (key_word[k] - 97)%26;
        key_word[k] = num;
    }
    else {
        int num = (key_word[k] - 65)%26;
        key_word[k] = num;

    }
}

//get plain text from user
printf("plaintext:");
char *plain_text = get_string();

int j = 0;
//start looping through each letter
for (int i = 0; i < strlen(plain_text); i++){
    //check length of j counter for cipher key and reset if to large, otherwise use j
    if (j < strlen(key_word)){
        if (isupper(plain_text[i])){

             plain_text[i] = (((plain_text[i] -65)+key_word[j])%26+65);

        }
        else {
            plain_text[i] = (((plain_text[i] -97)+key_word[j])%26+97);
        }
        j++;
    }
    else {
        j = 0;
    }
    }
printf("ciphertext: %s\n", plain_text);
}
else {
    printf("error");
    return(1);
}

}

1

Buff! This is a hidden problem related to the preprocessing of the key. First of all, saying that trying to change a variable of type char * is not a good idea, however we can do this in vigenere because the function get_string () is using the dynamic allocation of memory with malloc (), well, this is a question apart. So what happens in your program? We have a problem when the key begins with the letter a, in your program you are converting the characters of the key between 0 and 25 (correct), and what value corresponds to the letter a ?, as you already know zero. Then you reassign it to the array (string) key_word. For example, if we have key_word = "abc", after the reassignment we have the following array key_word ['\0', '\1', '\2'] (zero is the character that tells us what the end is of string). And now what is the problem ?, the call to the function strlen, this function returns the number of characters of a string up to the end character of string '\0', so if your key starts with a it turns out that strlen = 0. You can draw conclusions yourself.

1
  • Thanks for that amazing summary. Leaving aside the first part of your answer which I didn't think of what so ever, I actually tested for strlen of my keyword because I had the feeling it was doing what you mentioned. Weirdly enough, strlen maintains a length of 3 throughout, which is where I got stumped, because I didn't think of the '\0' part - good catch!! – yehoshua zlotogorski Nov 18 '18 at 16:06

You must log in to answer this question.

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