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

int main(int argc, string argv[])
{   
    string a = argv[1];
    int s = strlen(a);
    int h = 0;
    int k = 0;
    if(argc != 2) 
    {
    printf("No keyword entered.\n");
    return 1;
    }
    do
    {
    if(!isalpha(a[h]))
    {
    printf("Keyword must be alphabetical.\n");
    h = h - h + s;
    k = k + 1;
    return 1;
    }
    h = h + 1;
    }while(h == s);
    string t = GetString();
    int i = 0;
    for (int n = strlen(t); i < n; i++)
    {
        if(isalpha(t[i]))
        {
            if(isupper(t[i]))
            {   
                int p = a[i];
                p = p - 65;
                int l = t[i];
                l = l - 65;
                int b = (l + p%s)%26;
                b = b + 65;
                char c = b;
                printf("%c", c);
            }
            if(islower(t[i]))
            {
                int p = a[i];
                p = p - 97;
                int l = t[i];
                l = l - 97;
                int b = (l + p%s)%26;
                b = b + 97;
                char c = b;
                printf("%c", c);
            }
        }
        else
        {
        printf("%c", t[i]);
        }
    }
    printf("\n");
}

I have no idea what is going on with this code. Here is check50 output. Any ideas?

:) vigenere.c exists

:) vigenere.c compiles

:( encrypts "a" as "a" using "a" as keyword

   \ expected prompt for input, not output of "Keyword must be alphabetical.\n"

:( encrypts "world, say hello!" as "xoqmd, rby gflkp!" using "baz" as keyword

   \ expected output, but not "xoskb, say igmjp!\n"

:( encrypts "BaRFoo" as "CaQGon" using "BaZ" as keyword

   \ expected output, but not "CaSDmn\n"

:( encrypts "BARFOO" as "CAQGON" using "BAZ" as keyword

   \ expected output, but not "CASDOP\n"

:( handles lack of argv[1]

   \ expected output, not standard error of "/opt/sandbox50/bin/run.sh: line 31:

   6..."

:) handles argc > 2

:( rejects "Hax0r2" as keyword

   \ expected output, not a prompt for input

https://sandbox.cs50.net/checks/55b0306dc27646cbae4ab088d8834742

2 Answers 2

1

The first problem is that the code that checks whether the key is alpha has problems. It only checks the first character and only when the key is over 1 character long. If the key is only one character, the code will check the second "character" which is the null string terminator, so it tests as non-alpha. When you find it, it will be obvious, but until you see it, you may get really frustrated. I could tell you but you'll learn more if you find it yourself.

Second, you are indexing both the key and the uncoded string on the same variable. The problem specification says not to increment the key when processing non-alphas (spaces, numbers, punctuation, etc.) in the uncoded string, so this doesn't work. It will get the key out of sync with the alpha text.

Third, your encryption routine is incorrect. The encoding is tied to the key length in such a way that a key of "a" will produce one reslut, but "aa" will produce a different encryption. There are other issues, but the botton line is that it needs to be fixed.

Fourth, your code doesn't deal with any case where the key is upper case and the uncoded character is lower case or vice versa. If the encryption were otherwise working, it would only work if the key and uncoded letter were the same case.

Some of these problems will also lead to a segmentation fault during execution for certain inputs, as demonstrated by check50.

That should get you started.

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

-1

Hmm... how does it look now? I think I have fixed it all...

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

int main(int argc, string argv[])
{   
    string a = argv[1];
    int s = strlen(a);
    if(s == 0)
    {
    printf("No keyword entered\n");
    return 1;
    }
    for(int k = 0;k < s;k++)
    {
    if(!isalpha(a[k]))
    {
    printf("Keyword not alphabetical\n");
    return 1;
    }
    }
    string t = GetString();
    int i = 0;
    int x = 0;
    int v = 65;
    for(int n = strlen(t); i < n; i++)
    {
        if(isalpha(t[i]))
        {
            if(isupper(t[i]))
            {   
                if(islower(a[x]))
                {
                v = v + 32;
                }
                int p = a[x];
                p = p - v;
                int l = t[i];
                l = l - 65;
                int b = (l + p[s%x])%26;
                b = b + 65;
                char c = b;
                printf("%c", c);
                x = x + 1;

            }
            if(islower(t[i]))
            {
                int p = a[x];
                p = p - v;
                int l = t[i];
                l = l - 97;
                int b = (l + p[s%x])%26;
                b = b + 97;
                char c = b;
                printf("%c", c);
                x = x + 1;
            }
        }
        else
        {
        printf("%c", t[i]);
        }
    }
    printf("\n");
}

You must log in to answer this question.

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