0

Thanks in advance for your help. After completing Substitution, I ran check50 in terminal and was surprised to see that I had failed 4 of the checks. After debugging I located the source of the error. As you will see in my code, I first attempted to just print the ciphertext to the terminal at the very end of main. However, I found that when I typed out the alphabet in reverse of the "key" there was an extra character ("@") printed out at the end of the string. I was able to resolve this by reformatting my print statement. However, I was hoping that someone may be able to shed some light as to why my print statement added an additional character to the end of the string outside of the for loop?

    string ptext = get_string("plaintext: "); // Get plaintext from user.

    int length = strlen(ptext); // Length of plaintext array.
    int letter; // Used to store int value of char.
    int index; // Variable to store index position of letter in alphabet.
    char ctext[length]; // Create ciphertext array.

    printf("ciphertext: ");

    for (int i = 0; i < strlen (ptext); i++)
    {
        if (isalpha(ptext[i]) && isupper(ptext[i]))
        {
            letter = (int) ptext[i]; // Get int value of char.
            index = letter - 65; // Calulate index position of char in alphabet.
            ctext[i] = (char) (letter + letterdiff[index]); // Encrypt plaintext to ciphertext for uppercase.
        }
        else if (isalpha(ptext[i]) && islower(ptext[i]))
        {
            letter = (int) ptext[i]; // Get int value of char.
            index = letter - 97; // Calulate index position of char in alphabet.
            ctext[i] = (char) (letter + letterdiff[index]); // Encrypt plaintext to ciphertext for uppercase.
        }
        else // Otherwise pass original char back to string.
        {
            ctext[i] = ptext[i]; // Assign original value to original position in array.
        }
        printf("%c", ctext[i]);
    }


    // printf("ciphertext: %s", ctext);
    printf("\n");

    return 0;
}
1

To make a long story short (for me :-) ): extra characters in Vigenere pset2 2016

Instead you can type in "pset2 extra characters" as keywords for the search.

I guess, it was the same problem.

P.S.: If this helps you to solve "the problem", do not forget to check the thread as answered. Thx

You must log in to answer this question.

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