0

what's up? I'm having an issue debugging this code. So for I have been working on Vigenere 2 days (-school time) and works great except for keywords with capitalized letters like BaZ, BAR...

I'm guessing the answer lies on * char c = key[p] < 26 ? key[p] - 'A' : key[p] - 'a'; * because my- Vigenere.c encrypts correctly other combinations of and plaintext but not when it comes to the CAPS of the keyword.

Let me know if you have an idea on how to fix this

    /// Rotation of words according to <keyword>
    for(int i = 0, j = 0, length = strlen(key), n = strlen(text); i < n ;i++, j++)
    {
        int p = j % length; // OUTSIDE: To be modified for (else)
        if ('a' <= text[i] && text[i] <= 'z') 
        {
            char c = key[p] < 26 ? key[p] - 'A' :  key[p] - 'a'; // INSIDE: To reduce processes in case text[i] it's a character.
            c = 'a' + (text[i] - 'a' + c) % 26;
            printf("%c", c);
        }
        else if ('A' <= text[i] && text[i] <= 'Z')
        {
            char c = key[p] < 26 ? key[p] - 'A' :  key[p] - 'a'; // INSIDE: To reduce processes in case text[i] it's a character.
            c = 'A' + (text[i] - 'A' + c) % 26;
            printf("%c", c);
        }
        else
        {
            printf("%c", text[i]);
            j--;
        }
    }
    printf("\n");
    return 0;

https://sandbox.cs50.net/checks/55155b17520345ffaddc580e7fa776c8

1

I see you're incrementing j at the end of every iteration of the for loop then decrementing it in case the current char is not alphabetical. well, you could increment it only when you need to, right?

are you sure you wanna check whether key[p] is less than 26? also do you really have to do that every time either of the first two condition is true?

| improve this answer | |
  • Right, j can be modified only when needed. – Guillermo Alcántara González Nov 20 '15 at 16:11
  • My idea doing ` char c = key[p] < 26 ? key[p] - 'A' : key[p] - 'a' ` is that already know is a letter. Now I have to make 'a', 'A' = 0. since 'A' is equivalent to 65 in ASCii and 'a' to 97, I can only get the difference of key[p] - 'A' if this is < 26 then key[p] is between A and Z. In other case is a lower letter, I do the difference and I store the shifting letter value as c. With A=0, B=1, C=2.... Y=24, z=25. Or that' the idea. – Guillermo Alcántara González Nov 20 '15 at 16:22
  • @GuillermoAlcántaraGonzález you remember key is also a string and therefore it contains chars, right? assuming you ensured the chars in key are alphabetical, are you sure a char key[p] in key is gonna be mapping to an integer value less than 26? – Kareem Nov 20 '15 at 16:51
0

Here's the ternary operator's correct syntax:

http://cprogramminglanguage.com/c/c-operators-expressions/c-conditional-operators/

| improve this answer | |

You must log in to answer this question.

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