0

I looked through stack exchange, and it helped me understand a different way of indexing though the keyword. But it is still the wrong output. This is the first part of my for loop. It looks right and makes sense to me, but the output is "Sgsg mg nu vvr pcfx av rmexsa ao." instead of "Negh zf av huf pcfx bt gzrwep oz."

for(int i = 0, j = 1, m = strlen(p); i < m; i++, j++)
{

        if(isalpha(p[i]))
        {
            if(isupper(p[i]))
            {
                kl = strlen(argv[1]);
                int answer1 = ((p[i] - 'A') + (argv[1][j % kl] - 'A')) %26 + 'A';

                printf("%c",answer1);
                /*
                *The ASCII integer of A must be subtracted from the ith character in the plaintext in order to make
                *that character 0 and then later added back in order to give its ASCII value back. 
                */
            }

Where am I going wrong?

1

There might be several issues here.

  1. looping through your key

You're using "j" in your outer for loop to ensure doing this. I'm not sure why you initialise it with 1, though. More importantly, however, "j" gets incremented every time your outer loop executes, which is not what you want. Instead, you want to loop through your key ONLY IF the condition

if(isalpha(p[i]))

is met. So, the first thing you have to do is to think about a way that ensures that you only move up one letter in your key if the character you're encoding is indeed alphabetical. You could do this, for example, by writing something like

int k = j % strlen(argv[1]); // ensure looping of key

directly after

if(isalpha(p[i]))

is met. Now, you would be using

argv[1][k]

to select the character of your key that is going to be used for encryption. Importantly, to prevent looping through your key when plain[i] is not alphabetical, try to use something like

else
{
    printf("%c", plain[i]);
    j--;
}

in your for loop; the "else" statement in this case would be executed if plain[i] is not alphabetical. You decrease the value of j by one, which is then increased by one again immediately after your outer for loop has been executed again, ensuring that you remain at the same position in your key.

  1. Are you checking for uppercase and lowercase letters in your key?

You seem to be subtracting 65, or 'A', from every letter in your key. Think about including something like

if (isupper(argv[1][j]))
{
...
}

else if (islower(argv[1][j]))
{
...
}

after you have checked for upper and lower case of your plain text.

2
  • So I'm still having issues. I did what you suggested but I wasn't getting the same answer as Zamayla when I entered "Meet me at the park at eleven am." Then I added //you have to add the following code so that j loops back to 0 because it is incremented each time // the for loop initiated. k can then be moduloed. if(j == 5) { j = 0; } At first it worked for ./vigenere bacon but no other keyword. so I changed j == strlen(argv[1]). I got a segmentation default core dump. I changed it back, but now everything is a core dump...
    – Anissa
    Sep 18 '14 at 1:21
  • Can you send me a link to your code, or to Patrick.Kraetschmer@gmail.com?
    – pat_krat
    Sep 19 '14 at 17:43

You must log in to answer this question.

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