0

my code is giving output as expected but check50 says wrong output. here is my code and check50 log.

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

bool only_digits(string s);
char rotate(char c, int k);

int k, flag = 0;

int main (int argc, string argv[])
{
    // Check if there is only one argument after program name.
    if (argc != 2)
    {
    printf("Usage: ./caesar key\n");
    return 1;
    }
    else if (only_digits(argv[1]))  // check if argv[1] string consists only of numbers.
    {
    k = atoi(argv[1]);
    flag = 1;
    }
    else
    {
    printf("Usage: ./caesar key\n");
    return 1;
    }

if (flag == 1)
{
    // Ask user for plaintext
    string ptext = get_string("plaintext: ");

// Convert ptext to ctext.
    printf("ciphertext: ");

    for (int i=0; i <= strlen(ptext); i++)
    {
        printf("%c", rotate(ptext[i], k));
    }
    printf("\n");
}
}



bool only_digits(string s)
{
int flag2 = 0;
for (int i = 0; i < strlen(s); i++)
{
if (!isdigit(s[i]))
{
flag2 = 1;
}
}
if (flag2 == 1)
{
    return false;
}
else
{
    return true;
}
}

char rotate(char c, int key)
{
char cout;
     if (isupper(c))
        {
        cout = (c + key - 65) % 26 + 65;
        }
        else if (islower(c))
        {
        cout = ((c + key - 97) % 26 + 97);
        }
        else
        {
        cout = c;
        }
        return cout;
}


and the check50 log is


:) caesar.c exists.
:) caesar.c compiles.
:( encrypts "a" as "b" using 1 as key
    expected "ciphertext: b\...", not "ciphertext: b\..."
:( encrypts "barfoo" as "yxocll" using 23 as key
    expected "ciphertext: yx...", not "ciphertext: yx..."
:( encrypts "BARFOO" as "EDUIRR" using 3 as key
    expected "ciphertext: ED...", not "ciphertext: ED..."
:( encrypts "BaRFoo" as "FeVJss" using 4 as key
    expected "ciphertext: Fe...", not "ciphertext: Fe..."
:( encrypts "barfoo" as "onesbb" using 65 as key
    expected "ciphertext: on...", not "ciphertext: on..."
:( encrypts "world, say hello!" as "iadxp, emk tqxxa!" using 12 as key
    expected "ciphertext: ia...", not "ciphertext: ia..."
:) handles lack of argv[1]
:) handles non-numeric key
:) handles too many arguments

Please advise.

2 Answers 2

0

check50 is giving the correct output. It "sees" characters that you don't see because they are unprintable. If you want to see what check50 "sees" try this:

./caesar 3 | cat -v

with "abc" as the plaintext input. The -v switch to cat shows non-printing characters.

The problem is here: for (int i=0; i <= strlen(ptext); i++). Think about where the non-printing characters show when the output is piped through cat. Can you see what might cause it? If not, there's more detail in the spoiler.

It is ciphering the terminating null byte of the plain text, which renders a non-printing character.

0

perhaps some little mistake cause this problem.check the URL that check50 gives you and se the actual output .and see the difference. in sentimental-mario-more i had same problem when i checked the url i saw that my app.py makes extra break line . so check the URL and send us the screen shot

You must log in to answer this question.

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