0

i tried a lot to know what's the problem, but I can't.

    if (check_argc(argc) ==true)            // check if argc = 2
    {
        string k = argv[1];               // k is key

    if (check_key(k)==true)                 // check if key is valid
    {
         string p = get_string();

        for (int i = 0, j = 0, n = strlen(p), k_len = strlen(k); i<n && j < k_len; i++)
        {
            if (isalpha(p[i]))
            {
                if (islower(p[i]))
             {
                int add = (k[j]-'a') % strlen(k);
                int trans_to_alpha = p[i] - 'a';
                int sum = ((trans_to_alpha + add)%26) + 'a';
                printf("%c", sum);
             }
            else if (isupper(p[i]))
             {
                 int add = (k[j]-'A') % strlen(k);
                 int trans_to_alpha = p[i] - 'A';
                 int sum = ((trans_to_alpha + add)%26) + 'A';
                 printf("%c", sum);
             }
               j++;
            }
            else
            {
                printf("%c", p[i]);
            }
        }

        printf("\n");
    }
    }
    else
    {
        printf("Usage: ./vigenere k\n");
        return 1;
    }
}


    bool check_argc(int a)                   // check if argc is valid
{
    if (a == 2)
    {
        return true;
    }
    else
    {
        return false;
    }
}


bool check_key(string k)
{
    if (isalpha(k))
    {
        return true;
    }
    else
    {
        return false;
    }
}

can someone help me ,please?

1

isalpha() do not accept a pointer but only characters, hence segfault. You must write isalpha(k[i]),the simplest thing is to use a for loop to iterate over each character, besides there are other errors but I do not want to steal your fun

1
  • Thank you, I forgot that.
    – SamGal
    Mar 23 '17 at 19:23

You must log in to answer this question.

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