0

Code:

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

int main (int argc, char *argv[])
{
    if ( argc != 2 )
    {
        printf ("ERROR 1\n");
        return 1;
    }

    bool is_valid = true;

    for ( int i = 0, n = strlen (argv[1]); i < n; i++ )
    {
        if ( ! isalpha (argv[1][i]) )
        {
            is_valid = false;
            break;
        }
    }

    if ( ! is_valid )
    {
        printf ("ERROR 2\n");
        return 2;
    }

    string key = argv[1];
    string plaintext = GetString ();

    if ( strlen (key) >= strlen (plaintext) )
        {
            for ( int i = 0, n = strlen (plaintext); i < n; i++)
            {
                int c = 0;

                if ( isupper (plaintext[i]) )
                {
                    if ( isupper (key[i]) )
                    {
                        c = (((int) plaintext[i] - 'A' + (key[i] - 'A')) % 26) + 'A';
                        printf ("%c", (char) c);
                    }
                    else
                    {
                        c = (((int) plaintext[i] - 'A' + (key[i] - 'a')) % 26) + 'A';
                        printf ("%c", (char) c);
                    }
                }
                else if ( islower (plaintext[i]) )
                {
                    if ( isupper (key[i]) )
                    {
                        c = (((int) plaintext[i] - 'a' + (key[i] - 'A')) % 26) + 'a';
                        printf ("%c", (char) c);
                    }
                    else
                    {
                        c = (((int) plaintext[i] - 'a' + (key[i] - 'a')) % 26) + 'a';
                        printf ("%c", (char) c);
                    }
                }
                else
                    printf ("%c", plaintext[i]);
            }
        }
    else
    {
        for ( int i = 0, j = strlen (key), n = strlen (plaintext); i < n; i++)
        {
            int c = 0;

            if ( isupper (plaintext[i]) )
            {
                if ( isupper (key[i % j]) )
                {
                    c = (((int) plaintext[i] - 'A' + (key[i % j] - 'A')) % 26) + 'A';
                    printf ("%c", (char) c);
                }
                else if ( islower (key[i & j]) )
                {
                    c = (((int) plaintext[i] - 'A' + (key[i % j] - 'a')) % 26) + 'A';
                    printf ("%c", (char) c);
                }
            }
            else if ( islower (plaintext[i]) )
            {
                if ( isupper (key[i % j]) )
                {
                    c = (((int) plaintext[i] - 'a' + (key[i % j] - 'A')) % 26) + 'a';
                    printf ("%c", (char) c);
                }
                else if ( islower (key[i & j]) )
                {
                    c = (((int) plaintext[i] - 'a' + (key[i % j] - 'a')) % 26) + 'a';
                    printf ("%c", (char) c);
                }
            }
            else
                printf ("%c", plaintext[i]);
        }
    }

    printf ("\n");

    return 0;
}

The problem I have is with the second for loop. In particular this if statement: else if ( islower (plaintext[i]) )

There's something wrong with the code block inside. Running the check:

check50 2015.fall.pset2.vigenere vigenere.c
:) vigenere.c exists
:) vigenere.c compiles
:) encrypts "a" as "a" using "a" as keyword
:( encrypts "world, say hello!" as "xoqmd, rby gflkp!" using "baz" as keyword
   \ expected output, but not "xoqd, zz flk!\n"
:( encrypts "BaRFoo" as "CaQGon" using "BaZ" as keyword
   \ expected output, but not "CaQGn\n"
:) encrypts "BARFOO" as "CAQGON" using "BAZ" as keyword
:) handles lack of argv[1]
:) handles argc > 2
:) rejects "Hax0r2" as keyword

I wanna focus on :( encrypts "BaRFoo" as "CaQGon" using "BaZ" as keyword \ expected output, but not "CaQGn\n"

I know there's some parts missing, but i want to solve this before moving on to anything else. Can somebody point me in the right direction? Help would be much appreciated.

0

You have two problems. First, you have two typos. What do you think the code islower (key[i & j]) evaluates to? Fixing these gets you closer, at least giving the correct number of characters translated.

Second, and more significantly, is a very common problem. You cannot use the same index, i, for both the key and the plaintext. The plaintext index will be incremented for EVERY character, while the key index will ONLY be incremented when a letter is encoded. Because you are using the same index for both, the encoding gets out of synch. Coincidentally, or purposefully, one of the tests is designed so that the first and third words will translate correctly, but the middle word will be wrong when this flaw is revealed.

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 .