0

Took me awhile to figure out the logic behind this project. I thought I had it all worked out but I keep failing one check.

:( encrypts "BaRFoo" as "CaQGon" using "BaZ" as keyword \ expected output, but not "CaQGoh\n"

My code is below. Can someone please help me understand where my mistake is?

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


    int main(int argc, string argv[])

   {


        if (argc != 2)
      {    
           printf("Invalid input!!! Requires a \"String\" as a command line parameter\n");
return 1;
         }

      else

     {
         for (int i = 0, s=strlen(argv[1]); i < s; i++)
     {        
    if (!isalpha(argv[1][i]))
     {        
         printf("You must enter letters only for your key\n");

           return 1;


      }    
       }
        }

    string key = argv[1];

    char* pltxt = GetString();

    int key_index = 0;    
    int j = 0;

    for (int i=0, n =strlen(pltxt); i<n; i++)

        {
            if (isalpha(pltxt[i]))
          {
          {  
             key_index = j % strlen(key);
          }
            if  islower(pltxt[i])
           { 
             int a = ((pltxt[i] -97 +key[key_index] -97 ) %26 + 97);

             printf("%c", a);
          }


             else if isupper(pltxt[i])
          {    
             int b = ((pltxt[i] -65 +key[key_index] - 65 ) %26 + 65); 

             printf("%c", b);
          }

             j++;
        } 
            else 
            {    
                printf("%c", pltxt[i]); 
            }





        }

           printf("\n");
    return 0; 

}

1
  • As a side note, please spend some quality time with style50 and practice formatting your code to make it more readable. As posted, it has an excess of blank lines and poor indentation, especially curly braces. It makes it difficult for others to read.
    – Cliff B
    Commented Sep 19, 2016 at 23:53

1 Answer 1

0

The problem is that the code assumes that the case of the plain text and the key are the same. It doesn't handle the conditions where one is upper case and the other is lower case.

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 .