0

:( encrypts "world, say hello!" as "xoqmd, rby gflkp!" using "baz" as keyword \ expected output, but not "xoqmd, szz gflkp!\n" :( encrypts "BaRFoo" as "CaQGon" using "BaZ" as keyword \ expected output, but not "CaQGoh\n"

My code meets every other check except these 2. I realize they may be(and probably are) two different problems. My outputs are just so close to the correct ones that its hard for me to find the error. Like why is 'say' the only word that is wrong in the sentence? Or why is CaQGon made into CaQGoh? Why the h?

    {
string key = argv[1];

if ((argc != 2))
{
    printf("Please enter a correct key!\n");
        return 1;
}

for (int i = 0, n = strlen(key); i<n; i++)
{
    if (!isalpha(key[i]))
    {
        printf("Please enter a correct key!\n");
        return 1;
    }
}   
string p = GetString();

for (int i = 0, j = 0, n = strlen(p); i < n ; i++)
{
    if (isupper (p[i]))
    {
        printf("%c", ((((p[i] - 'A') + (key[i % strlen(key)] - 'A')) % 26) + 'A'));
        j++;

    }
    else if (islower (p[i]))
    {
        printf("%c", ((((p[i] - 'a') + (key[i % strlen(key)] - 'a')) % 26) + 'a'));
        j++;
    }
    else
    {
        printf("%c", p[i]);
    }
}
    printf("\n");
    return (0);

Thank you in advance!

1

Two problems. Actually, two of the three most common problems. First, you are using the same index, i, for both the plain text and the key. The key index should only be advanced when a letter is encoded, and not when a non-alpha is processed. Instead, your code advances through the key whenever a space, number, or any other non-alpha is processed, so it gets out of synch. It just happens that the input data for the test will only cause that one word to be out of synch, but comes back into synch after that word.

Next, based on the result (since the code for this wasn't posted), it looks like you are accounting for the case of the plain text, but not for the key. Simply put, your code assumes that the case of both are the same (2 of 4 possible cases), but doesn't handle the case where one is uppercase and the other is lowercase.

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

5
  • thanks for the response. i only increments in the for loop, but j increments only if isupper and if islower. Is that not separating them correctly?
    – Jared
    Apr 3 '16 at 5:09
  • Look at the code: printf("%c", ((((p[i] - 'A') + (key[i % strlen(key)] - 'A')) % 26) You're not using j, just incrementing it
    – Cliff B
    Apr 3 '16 at 5:24
  • Sorry if this is obvious but, { printf("%c", ((((p[i] - 'a') + (key[i % strlen(key)] - 'a')) % 26) + 'a')); j++; } I am using j, its on the line right beneath it, my thoughts is once I print a character, I increment j, should they be in the same line of code?
    – Jared
    Apr 3 '16 at 5:24
  • key[i....]!!!!!
    – Cliff B
    Apr 3 '16 at 5:25
  • AHHHHH!!!! Thank you!!! I will work on the case matching some more before I ask again!
    – Jared
    Apr 3 '16 at 5:27

You must log in to answer this question.

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