0

I've asked a question regarding Vigenere here before, and updated my code according to the answer, but still can't seem to get it to work.

#include <cs50.h>
#include <string.h>
#include <stdio.h>
#include <ctype.h>
#include <stdlib.h>

int main (int argc, string argv [])
{

    if (argc !=2)
    {
        printf ("Error. Instructions not followed.\n");
        return 1;
    }

    string key = argv[1];
    for (int i = 0, n = strlen (key); i < n; i++)
    {
        if (!isalpha(key[i]))
        {
            printf ("Non-alphabetical characters.\n");
            return 1;
        }
    }
    printf ("plaintext: ");
    string plaintext = get_string();
    printf ("%s\n", plaintext);
    printf("ciphertext:");
    for (int k = 0, n = strlen (plaintext); k < n; k++)
    {
        for (int g = 0, v = strlen(key); g < v; g++)
        {
            char keyl = (key[(g)%v]);
            if (isalpha(plaintext[k]) && isalpha(keyl))
            {
                if (isupper(plaintext[k]) && isupper(keyl))
                {
                    printf("%c", (((((plaintext[k])-65)+(keyl-65))%26)+65));
                }
            }
        }
    }
}

I know the formula for the encryption itself is correct, because I've done it by hand. The code has several problems with it:

  1. The outputted ciphertext exceeds the string length of the plaintext.
  2. The encryption doesn't work as planned, not for any letters, although the formula works on paper.

That's about as much as I know. If I'm struggling this much with a problem so simple, is it best that I walk away?

1
  • Frustrated? Of course you are. You're new to this. But keep at it. Your reaction is very common. The good news is this. If you make lots of mistakes now and resolve them, you'll develop an instinct for debugging software and it will make creating good code in the future much easier in the future. Happy coding!
    – Cliff B
    Sep 15 '17 at 19:14
1

I'll give the answer according to the numbering of the issues.

  1. The basic issue is the nested for loop. If you try to find the number of iterations performed by this loop, it is strlen(plaintext) * strlen(key). Further, your if conditions mean that a character gets printed maximum for every iteration. So, clearly maximum number of characters getting printed are same as number of iterations while it should be just strlen(plaintext) in all cases. You need a single for loop (think about it).
  2. The conditions which you check in your if conditions don't seem to be correct. The plaintext characters don't need to be uppercase, which you seem to be checking using isupper(plaintext[k]);. Same for the key. Also, I can't understand what you want to achieve using char keyl = (key[(g)%v]);. Further, you do not print the character as it is in case of non alphabetical character in the plaintext, which is required in the specifications.

Note that you need to follow the specifications to get the problem correct. Finally, about your last question, don't give up, even if this answer doesn't help you. Keep trying, re-watch the lecture videos and the shorts(if you haven't already) as this will help you immensely.

You must log in to answer this question.

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