0

My code works with a Key of BBB and the letters AAA and loops properly and skips spaces, numbers, punctuation but fails the tests. With a key of baz, it turns world say hello to xpsme, tbz ifmmp and not xoqmd, rby gflkp?!

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


int main(int argc, string argv [])

{ 
                //* end program if there is not 2 arguments
                if (argc != 2)
                { printf ("Needs an argument!\n");
                return 1;
                };

    //* key is the key for encyption
     string key = argv[1];

    int sk = strlen(key);
    int n = 0;

    //* go through each letter in the key and check it is an alpha letter
    for (int t =0; t < sk; t++)
    {
        if (isalpha(key[t]))

        {

            //* letter is the code to encypt
            string letter = GetString();

            int sl = strlen(letter);


            //* go through each character in the letter until the end. use modulo of key length to loop around the key.
            for (int i= 0, j = n% sl; i< sl; i++)
                {

                //* if the character in the letter is upper case
                if (isupper(letter[i]))

                        //* use alphabet numbers to change the characters and then use ASCII to convert to letters.
                        {
                        int new_letter = ((letter[i] - 65)+ (key[j]- 65) )%26;
                        printf ("%c", new_letter + 65 );
                        n++;

                        }

                //* if the character in the letter is lower case
                else if (islower(letter[i])) 
                        {
                        int new_letter = ((letter[i] - 97)+ (key[j] - 97))%26;
                        printf ("%c", new_letter + 97 );
                        n++;

                        }




                //* if the character is not a letter, print as is
                else 
                {
                printf ("%c", letter[i]);

                }




            }     

                 //* Print a new line and end program
                 printf ("\n");
                 return 0;



         }        
       //* if the key is not alpha, end program
        else 
        {
               return 1;
        }

    }    



}

Thanks for any help!

1
  • So I realised the problem is that it is't looping properly. I just uses the first letter in the key and uses that to change the message characters. But I'm not sure why that is....?
    – Elizabeth
    Jul 10 '16 at 14:55
0

There are multiple problems. As MARS said, you haven't coded for the situation where the case of the key and the plain text are not the same. But the bigger problem is how you are trying to move through the key. In one place, you're tying the key index to the length of the plaintext. Why? The key should wrap back to 0 based on the length of the key. Next, the code increments n after encoding a letter, not j, so it never uses any letter beyond the first in the key. (In a for loop setup, once the initialization has occured, the initialization clause is never revisited and has no further effect.)

In short, the code that is changing the index for the key is too complex and depends on the wrong data. All it needs to do is start at 0, increment only when a letter is encoded, and go back to 0 when the end of the key is reached.

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

0
if (isupper(letter[i]))

.....

else if (islower(letter[i]))

.....

you do not consider the formulas when

 if (isupper((key[i]))...

You must log in to answer this question.

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