I won't post my code, because, as far as I could tell, it works fine, but I keep on getting this when I run check50 and I don't know know exactly what it means or how to fix it. My code runs perfectly whatever input I give it, as far as I know.
:) caesar.c exists.
:) caesar.c compiles.
:( encrypts "a" as "b" using 1 as key
output not valid ASCII text
:) encrypts "barfoo" as "yxocll" using 23 as key
:) encrypts "BARFOO" as "EDUIRR" using 3 as key
:) encrypts "BaRFoo" as "FeVJss" using 4 as key
:) encrypts "barfoo" as "onesbb" using 65 as key
:( encrypts "world, say hello!" as "iadxp, emk tqxxa!" using 12 as key
output not valid ASCII text
:) handles lack of key
:) handles non-numeric key
:) handles too many arguments
I just ran my code again and realized that when it runs it sometimes adds another character after the output. Maybe that's what the error is?
Thanks for the past answers. They really helped.
Hi, here's my code. Maybe it will help.
#include <stdio.h>
#include <cs50.h>
#include <string.h>
#include <ctype.h>
#include <stdlib.h>
int main(int argc, string argv[])
{
//stop if more than one key and if not a digit
if (argc != 2)
{
printf("Usage: ./caesar key\n");
return 1;
}
for (int n = 0; n < strlen(argv[1]); n++)
{
if (isalpha(argv[1][n]))
{
printf ("Usage: ./caesar key\n");
return 1;
}
}
int key = atoi(argv[1]);
//ask user for string
string ptxt = get_string("Plaintext:");
//initialize the char ctxt to be as long as the string ptxt
char ctxt [strlen(ptxt)];
//run program, but keep uppercase/lowercase and non alphabetical characters
for (int i = 0; i < strlen(ptxt); i++)
{
if (isalpha(ptxt[i]))
{
if (isupper(ptxt[i]))
{
ctxt[i] = (((ptxt[i] - 65 + key) % 26) + 65);
}
else if (islower(ptxt[i]))
{
ctxt[i] = (((ptxt[i] - 97 + key) % 26) + 97);
}
}
else
{
ctxt[i] = ptxt[i];
}
}
//print the ciphertext
printf("ciphertext: %s", ctxt);
printf("\n");
return 0;
}
Thanks!