0

I used check50 on my caesar code but did not get the correct output. Below is the output and the code. I put a printf("\n"); at the end of the int(main) code, and also the end of my else statement, but I still get the same error. Any tips?

:) caesar.c exists
:) caesar.c compiles
:( encrypts "a" as "b" using 1 as key
   \ expected output, but not "cyphertext: b\n"
:( encrypts "barfoo" as "yxocll" using 23 as key
   \ expected output, but not "cyphertext: yxocll\n"
:( encrypts "BARFOO" as "EDUIRR" using 3 as key
   \ expected output, but not "cyphertext: EDUIRR\n"
:( encrypts "BaRFoo" as "FeVJss" using 4 as key
   \ expected output, but not "cyphertext: FeVJss\n"
:( encrypts "barfoo" as "onesbb" using 65 as key
   \ expected output, but not "cyphertext: onesbb\n"
:( encrypts "world, say hello!" as "iadxp, emk tqxxa!" using 12 as key
   \ expected output, but not "cyphertext: iadxp, emk tqxxa!\n"
:) handles lack of argv[1]

Caesar Code

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


int main(int argc, string argv[])
    {

    //Trying to prevent user dfrom inputting alpha and != argc
    if(argc != 2)
        {

         printf("Try again\n");
         return 1;

        }

    else

    {

    //Converting string in argv[1] to int
        string k = argv[1];
        int p = atoi(k);



        printf("plaintext: ");
        string plain_text = GetString();
        printf("cyphertext: ");


        for(int i = 0, j = strlen(plain_text); i < j; i++)
            {
                int a = plain_text[i];

                if(isalpha(a) && isupper(a))
                {
                    int b = ((a - 65) + p) % 26;
                    char c = b + 65;
                    printf("%c", c);
                }

                else if(isalpha(a) && islower(a))

                {
                    int b = ((a - 97) + p) % 26;
                    char c = b + 97;
                    printf("%c", c);
                }

                else
                {
                    printf("%c", plain_text[i]);
                }
            }

     }

    printf("\n");
    return 0;

    }
0

Typo: ciphertext, not cyphertext

2
  • Thanks Blauelf! That did it! This whole time I thought it was how I implemented the code, but I should have looked at my spelling. Thanks again! Feb 18 '17 at 2:06
  • Blauelf You saved my life 2h and I couldn't find the mistake. I had Ciphertext instead ciphertext !!!! Thanks
    – Anna Niti
    Mar 3 '17 at 3:23

You must log in to answer this question.

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