0

I'm extremely new to coding so bear with me. With my code, it, unless both the key and the plaintext are lowercase garbage values, are printed ruining the encryption of the plaintext. I struggle to understand how this has occurred and tried everything i.e checking magic numbers, variables etc yet still once capital letters are involved it still prints these values. What is the issue with this code?

#include <stdio.h>
#include <cs50.h>
#include <string.h>
#include <stdlib.h>
#include <ctype.h>
//Bug-Only lowercase letters function well
  int shift(char c);
  int j = 0;

 int main(int argc, string argv[])
 {
//Making sure user imputs only 2 arguments
if (argc != 2)
{
   printf("Usage: ./vigenere keywordn");
   return 1;
}
// Checking if all the charcters in Argv are Alphabetical

else
{   
   for (int x = 0, n = strlen(argv[1]); x < n ; x++)
     {
        if (!isalpha(argv[1][x]))
           {
              printf("Usage: ./vigenere keyword\n");
               return 1;
            }
          }

      }

   //triggering for user input
 string text = get_string("Plaintext:");
 printf("ciphertext:");


    // Enciphering process, iterating over text length
    for(int i=0,s=strlen(text);i<s;i++)
            {    
              //checks whether it is upper case to preserve it
                if(isupper(text[i]))
                    {  
                        int key=shift(argv[1][j]);
                        printf("%c",((text[i]-65+key)%26)+65);


                    //bool to assess the current keyword variable length
                           if(j>strlen(argv[1]))
                           {
                               j=j-strlen(argv[1]);
                           }
                           else
                           {
                               j++;
                           }
                    }
         //checks whether it is lower case to preserve it
                if(islower(text[i]))
                    {
                        int key=shift(argv[1][j]);
                      printf("%c",((text[i]-97+key)%26)+97);
                    //bool to assess the current keyword variable length
                       if(j>strlen(argv[1]))
                           {
                               j=j-strlen(argv[1]);
                           }
                           else 
                           {
                               j++;
                           }
                    }
                else
                {
                    printf("%c", text[i]);
                }
            }
         }




     int shift (char c)
{
     if (isupper(c))
   {
       int k=c-65;
        return k;
   }
    if (islower(c))  
   {
         int k= c-97;
        return k;
   }        
  else
  {
      return 1;
  }

}

  • please edit the question and add your shift function. Also, what is the initialization of j? – Cliff B Feb 21 '19 at 2:26
  • j is initialized to 0 at the beginning of the code as a global variable, and i have edited the question to contain the full shift function and code. The logic seems to be right in my eyes but I've yet to understand the cause of the issue. – Keano Doyley Feb 23 '19 at 15:57
  • How does Your code works when j == strlen(argv[1])? In this case argv[1][j] will point to '\0'. – ArMANIAK Feb 23 '19 at 16:44
2

If you run the code with key = "bbb" and plaintext ABC, the result is ciphertext:BACBDC. This is a mix of the properly coded BCD and the original ABC, printing alternatingly from each string. This also shows that the code is printing two letters for each letter in the plaintext.

The code is structured as follows:

if(  <process UPPERCASE>  ) {...}

if(  <process lowercase>  ) {...}
else (  <process non-alpha>  )

Let's map out how that works. Say that the plaintext letter being processed is lowercase. The first if fails, as it should. The second if evaluates as true, so the letter is encoded. The else statement is skipped. All appears to be working correctly here.

Now, say that the plaintext is uppercase. The first if statement is evaluated as true, so the uppercase letter is encoded and printed. But here's where the problem happens. The next if statement is evaluated correctly as false. As a result, the else clause is executed and the original, unencoded plaintext char is printed. That's the second char that you are seeing printed.

The problem is the structure of IF - IF/ELSE that the code has. The two if statements are NOT linked to each other, but the second if statement and the else statement are linked. All three need to be linked together - IF/ELSE IF/ELSE.

There is another issue, but you should quickly find it. Or, it'll be for another question. ;-)

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

| improve this answer | |
  • Ahh makes sense the issue with the capitals seems to be corrected as shown by you the if and if else statements aren't connected leading to these being printed, but i have found the other issue with my code. When using key BBB and printing aaaaaa it comes out as bbbblb. Now from my understanding the L is a garbage value likely caused by the array but i do not understand the actual cause of the issue and how to fix it. – Keano Doyley Feb 24 '19 at 14:20
  • That would be for a new question. ;-) – Cliff B Feb 25 '19 at 0:49

You must log in to answer this question.

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