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.