1

something weird is going on with my code. I get the first word ("Meet" with key "bacon") right, but after that, things are getting really hairy. here is my code. I would really apreciate any help, I've been sitting on this problem for days already.

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

int main (int argc, string argv[])
{
    string key = "input";


        if (argc != 2)
        {
            printf ("please provide only one keyword (alphabetical)\n");
            return 1;
        }
        else if(argv[1])
        {
            for (int i = 0, n = strlen(argv[1]); i < n; i++)
            {
                if (!isalpha (argv[1][i]) )
                {
                    printf ("Please only letters in the keyword!\n");
                    return 1;
                }
                else
                {
                    key = argv[1];
                }
            }        
        }

        string p = GetString();
        int m_length = strlen(p);
        int k_length = strlen(key);


        for (int i = 0; i < m_length; i++)
        {
            if (isalpha(p[i]))
            {
                if (islower(p[i]))
                {
                    printf("%c", (((p[i] - 97) + ((key[(i % k_length)]) - 97)) % 26 + 97));
                }
                else
                {
                    printf("%c", (((p[i] - 65) + (toupper((key[(i % k_length)])) - 65) % 26) + 65));
                }   
            }   
            else
            {
                printf("%c", p[i]);
            }
        }

}

UPDATE: Thanks Cliff, I realized i also need to add "tolower" in order to handle the upper case key chars with lower case message chars. The code works now, but only without the spaces between the words. I want to just print them, but somehow the program doesn't handle it properly. Any thoughts?

for (int i = 0; i < m_length; i++)
        {
            if (isalpha(p[i]))
            {
                if (islower(p[i]))
                {
                    printf("%i ", ( (p[i]-97) + (tolower(key[(i%k_length)]) - 97) ) % 26 + 97);
                }
                else
                {
                    printf("%i ", ( (p[i]-65) + (toupper(key[(i%k_length)]) - 65) ) % 26 + 65);
                }   
            }   
            else
            {
                printf("%c", p[i]);
            }
        }

2 Answers 2

1

There are two issues with your code, all related to upper case letters. The obvious one to you is that it isn't encoding upper case letters correctly. The reason is subtle. Your logic is correct, but while coding it, you didn't get all, or enough of the ()'s in the right places. Very simply, you are doing this:

(p[i] - 65) +    [  (toupper((key[(i % k_length)])) - 65) % 26) ]

Note that the % 26 is only being applied to the key, not (p[]+key[]), as highlighted by my addition of the brackets. I'll leave it to you to figure out the fix.

Second, something you may not have picked up on. You're handling upper and lower case chars to be encrypted, but you're not handling upper and lower case keys correctly. What happens when you have an upper case key and a lower case letter to encrypt?

If you need more help, post an additional comment or a new question, if it's complicated.

If this answers your question, please mark this as answered. Let's keep up on forum housekeeping. ;-)

1
  • thanks Cliff, would you mind to take a look at the updated question? Can't figure out how to handle the spaces properly.
    – Anton K
    Commented Apr 16, 2015 at 13:13
0

I figured it out. When the program prints a special char (such as a space) the index i is shifted. but it should stay the same in order to use the unused key char for the next alphabetical char in the message. I'm going to try to solve this problem by adding a new index variable for the key.

Thanks!

You must log in to answer this question.

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