0

I'm having some problems with making the vigenere assignment from CS50. I get many errors about my comparison between pointer and integer, how can I avoid this error? I also have some problems with the expression "if (strlen(plaintext(!=0)))", and don't seem to understand why.. Can you help me please?

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

int main(int argc, string argv[])
{
    // faults when no good arguments are given
    if (argc(! = 2))
    {
        printf("no good value entered!\n");
        return 0;
    }

    if (!isalpha(argv[1]))
    {
        printf("No good value entered!\n");
        return 0;
    }

    string key = (argv[1]);

    //get plaintext from user: repeat untill some characters are given
    do
    {
        printf("plaintext: ");
        string plaintext = get_string();
    }
    while (strlen(plaintext) == 0);

    // check if plaintext is given
    if (strlen(plaintext(! = 0)))
    {
        printf("no right characters are detected\n");
        return 0;
    }

    // encrypt plaintext 
    int keylenght = strlen(argv[1]);

    for (int i = 0, j = 0, n = strlen(plaintext); i < n; i++, j++)
    {
        // start key again if shorter than plaintext 
        if (j >= keylenght)
        {
            j = 0;
        }

        // skip key [j] if plaintext[i] is not alpha
        if (!isalpha(plaintext))
        {
            j = (j - 1);
        }

        // make Aa=0 and Zz=25 for uppercase
        if ((key >= 'A') && (key <= 'Z'))
        {
            key = (key - 'A');
        }

        if ((key >= 'a') && (key <= 'z'))
        {
            key = (key - 'a');
        }

        string shift = (plaintext[i] + key);

      // stopping at Z for uppercase
         if ((isupper(plaintext[i]) && (shift > 'Z'))
        {
            shift = (shift - 26);
        }

        //stopping at z for lowercase
        if ((islower(plaintext[i] && (shift > 'z'))
        {
            shift = (shift - 26);
        }

        // check if alpha --> print encrypted
        if (isalpha(plaintext[i]))
        {
            printf("%c", shift);
        }

        // non-alpha --> just print
        else
        {
            printf("%c", plaintext[i]);
        }
    }

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

What is ! = 2? Does not look like a valid expression to me.

Use if (argc != 2).

Instead of the other ! = 0, something like if (strlen(plaintext) != 0) would be correct, this condition would always be true at that point, since you can leave the do..while loop only if it is.

But I don't think you're supposed to do a do..while loop. I could imagine an empty string to be a valid input.

You have another problem here:

if (!isalpha(argv[1]))

isalpha takes a single character (which is of type char, an 8-bit integer), not a string (which is a pointer to char), so you'll have to create a loop, and if any of the characters in the string is not alphabetic, exit with an error message.

Also, you're using key for the string, not an individual character, you'll need separate variables for that!

Oh, and that typo keylenght drives me crazy. That must be OCD hell.

You must log in to answer this question.

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