0

Hello! I don't understand why my code won't work properly, I have done everything I could. I've attached the portion of my code that is involved with the ciphering process. I would really appreciate any help – I've been working on this for the past week and I can't seem to finish it. Thanks!

// DECLARE KEY FROM COMMAND LINE ARGUMENT
string key = argv [1];

// FIND LENGTH OF SUCH KEY
int length = strlen (key);

// CONVERT KEY TO SHIFT VALUE
for (int location = 0; location < strlen (key); location++)

{
    // CONVERT LOWERCASE LETTERS TO SHIFT VALUES
    if (islower (key[location]) && isalpha (key[location]))
    {
        key[location] = key[location] - 97;
    }

    // CONVERT UPPERCASE LETTERS TO SHIFT VALUES
    if (isupper (key[location]) && isalpha (key[location]))
    {
        key[location] = key[location] - 65;
    }
}

// ENCRYPT TEXT DEPENDENT UPON A PROPER KEY
if (argc == 2)

{
    // PRINT INPUT PROMPT
    printf ("plaintext: ");

    // RETRIEVE STRING FROM USER 
    string argument = get_string();

    // PRINT OUTPUT PROMPT
    printf ("ciphertext: ");

    // LOOP AROUND KEY
    int around = 0;

    // SHIFT LETTERS DEPENDENT UPON STRING
    for (int value = 0; value < strlen (argument); value++)
    {
        // PRESERVE LOWERCASE LETTERS IN SHIFT
        if (islower (argument[value]) && isalpha (argument[value]))
        {
            argument[value] = argument[value] - 97 + key[around % length] % 26 + 97;
            around++;
        }

        // PRESERVE UPPERCASE LETTERS IN SHIFT
        if (isupper (argument[value]) && isalpha (argument[value]))
        {
            argument[value] = argument[value] - 65 + key[around % length] % 26 + 65;
            around++;
        }

        // PRINT CIPHERTEXT
        printf ("%c", argument[value]);
    }

        // PRINT NEW LINE AND RETURN
        printf ("\n");

        return 0;
    }

}

1 Answer 1

0

I'll bet that each time you have an 'a' or 'A' in your key, it breaks down. Look at this line:

    for (int location = 0; location < strlen (key); location++)

On each pass, it calculates strlen(key) before doing the less than test. The problem here is that once you process an a or A, it stores 0 as the converted key. This happens to be the binary value of the end of string character, \0. So, when it loops back around and does the test, the location of the 'a' becomes an end of string marker and the loop terminates.

To fix this, calculate the strlen once and store it, as follows:

    for (int location = 0, n = strlen(key); location < n; location++)

This may or may not be the only issue. To be honest, I haven't done an in-depth analysis beyond this.

BTW, isupper() and islower() both automatically imply isalpha(), so the isalpha test is redundant.

If this answers your question, please click on the check mark to accept. Let's keep up on forum maintenance. ;-)

1
  • Thank you! I didn't know that.
    – Dilara
    Jun 1, 2017 at 1:38

You must log in to answer this question.

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