0

I'm trying to complete the vigenere code assignment but sadly, despite it seemingly working great with lowercase letters, it completely bugs out with capital letters.

This is how it looks like with lowercase:

(command line) - ./vigenerem BAZ
(input) - barfoobarfoobarfoo
(output) - caqgoncaqgoncaqgon

and this is how it looks like with capital letters:

(command line) - ./vigenerem BAZ
(input) - BARFOOBARFOOBARFOO
(output) - CAQG5N)'Q-5N)'Q-5N

It is worth noting that all other tests pass fine, it works in all cases except for this specific one, and I'm unable to see (despite sitting around 3 hours now on this specific bug) what causes the capital letters specifically to bug out completely on me.

After some debugging (I added it as a note (the else - buggio lines) I found out that apparently k[ki] is not considered to be an alphabet letter after running k amount of cycles (meaning, if the key is 3 letters long, on the 4th it'll stop the program and tell me that k[ki] is infact not a letter, weirdly enough.

And that's all I'm able to find out, after deleting this little debugging command, it all works as long as I'm not using capital letters.

Please help, I'm getting quite desperate =/

Here is the full code if it is of any help:

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

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

if (argc != 2)
{
    printf("You should be pointing out the key (in string) as a command line\n");
    return 1;
}

int ki = 0;
string k = argv[1];
int kl = strlen(k);

for (int ii = 0; ii < kl;ii++)
{
    if (isalpha(k[ii]) == 0)
    {
        printf("Sorry, I can only work with alphabetic letters\n");
        return 1;
    }
}

string p = GetString();

for (int i = 0, n = strlen(p); i < n; i++)
{
    ki = ki % kl;


    if (isalpha(p[i]))
    {
        if (isalpha(k[ki]))
        {
            if (isupper(k[ki]))
            {
                k[ki] = k[ki] - 'A';
            }
            else
            {
                k[ki] = k[ki] - 'a';
            }
        }
        //else
        //{
            //printf("\n%c\n",k[ki]);
            //printf("Hey it'sa me, Buggio!\n");
            //return 1;
        //}

        if (p[i] + k[ki] > 'z' || (p[i] + k[ki] > 'Z' && p[i] + k[ki] < 'a') || (isupper(p[i]) && p[i] + k[i] > 90))
        {
            p[i] = p[i] + k[ki] - 26;
            printf("%c", p[i]); 
        }
        else
        {
            printf("%c", p[i] + k[ki]);
        }
        ki++;
    }
    else
    {
        printf("%c", p[i]);
    }

}
printf ("\n");
return 0;
}

Thanks in advance and have a good day =o

0

This program needs some major rethinking. The following is the core of your code:

 if (p[i] + k[ki] > 'z' || (p[i] + k[ki] > 'Z' && p[i] + k[ki] < 'a') 
      || (isupper(p[i]) && p[i] + k[i] > 90))

It is testing the result of encoding characters, but it has problems.

If a char is not a letter, it should not be encoded at all. This doesn't check whether a char is an alpha or not.

As for encoding, there are a number of possibilities that can happen here - more than I wish to analyze right now. Simply put, it has problems.

My advice is to review the class material for this assignment and the pset instructions and try again. Also, review the docs on isalpha(), isupper(), islower(), toupper() and tolower(). I think you'll find them very useful.

Sorry if you don't find this very useful, but there's a lot of work to be done on this code.

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

You must log in to answer this question.

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