0

My program perfectly encrypts texts if user gives a key or enters a wrong key such as an alphabet. But if argc is 1, then I incur a segmentation fault. I also incur a few other problems when I apply check50 for which I have attached a Screenshot as well. I am confused as to what the program lacks. I'm also getting the following errors as follows:

OTHER ERRORS:

1) encrypts "a" as "b" using 1 as key
    expected "ciphertext: b\...", not "b\x00\n"

2) encrypts "barfoo" as "yxocll" using 23 as key
    expected "ciphertext: yx...", not "yxocll\x00\n"

3) encrypts "BARFOO" as "EDUIRR" using 3 as key
    expected "ciphertext: ED...", not "EDUIRR\x00\n"

4) encrypts "BaRFoo" as "FeVJss" using 4 as key
    expected "ciphertext: Fe...", not "FeVJss\x00\n"

5) encrypts "barfoo" as "onesbb" using 65 as key
    expected "ciphertext: on...", not "onesbb\x00\n"

6) encrypts "world, say hello!" as "iadxp, emk tqxxa!" using 12 as key
    expected "ciphertext: ia...", not "iadxp, emk tqx..."

7)handles lack of key
    failed to execute program due to segmentation fault

8) handles non-numeric key
    expected exit code 1, not 0

9) handles too many arguments
    expected exit code 1, not 0

enter image description here

My code is as follows:

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

int main(int argc, string argv[])
{
bool Count=true;
   if (argc > 1 && argc< 3)
     {
       Count=true;
     }
   else
     {
       Count=false;
       printf("Usage: ./caesar key\n");
     }

int length=strlen( argv[1] )  ;
   for( int i=0; i <= length ; i++ )// Loop to check each element of the Array
       {
         if ( ! isdigit( argv [1][length-1] ) )// Check which elements are Non Digits.
               {
                Count=false;
               }
       }

int const KEY=atoi(argv[1]);// User key is converted into an integer
         if(Count==true)
               {
                printf("Success\n");
                printf("%i\n", KEY);
               }

         if( Count==false)
               {
                 printf("Usage: ./caesar key\n");
               }

if(Count==true)
// To enter into Plaintext Encryption part
{
   string plaintextArray[10000];
// An array of 10000 element storage is declared
   int i;

   plaintextArray[1]=get_string("plaintext: ");
// Storing user input plaintext in an array [1]

   int n=strlen( plaintextArray[1] );
// Length of of the array

   for(i=0 ;i <=n; i++ )
// Loop to check each Char in the Plaintext
     {
      char c=plaintextArray[1][i];
//Takes each element of an array as char i.e (i)

      if( isalpha( c ) )
// If an alphabet is found, it enters the arguement
         {
            if( islower(c) )
//Shifts lowercase ASCII values using formula
              {
                 c=(c-97+KEY)%26+97;//Formula to convert ASCII values into Alphabetical Index
              }

             else if(isupper (c) )
//Shifts uppercase ASCII values using formula
              {
                 c=(c-65+KEY)%26+65;
//Formula to convert ASCII values into Alphabetical Index
              }
         }
             printf("%c",c);
// Prints Ciphertext after Plaintext Converted
      }

printf("\n");
       return 1;
}
}
0

The code returns a seg fault when there's no parameter given because the code doesn't terminate the program after detecting that argc is not 2. Add a return statement.

Also, the test can be simplified from the 9 lines of code above to this:

if (argc != 2)
{
    printf("Usage: ./caesar key\n");
    return 1;
}

Next, the code doesn't print "ciphertext: " before outputting the encoded text.

Also, all the test printf statements need to be removed in order to run check50.

You should spend some time looking over the code, looking for ways to streamline and simplify it.

Finally, please run the code through style50. It needs a lot of cleanup! submit50 does check style and deducts points for bad coding style.

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

2
  • But I am confused as to what you mean by "removing printf statements" – Raiyan Rabbani Sep 26 '20 at 15:51
  • You have some printf statements that you included, presumably for testing. Any extra output, like from these test printf statements (printf("Success"), etc), will cause check50 and submit50 tests to fail. – Cliff B Sep 26 '20 at 18:57

You must log in to answer this question.

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