My code compiles and it seems to handle the encryption of the given text correctly...but not according to the cs50 check. Here's the result of the check:
:) caesar.c exists
:) caesar.c compiles
:) encrypts "a" as "b" using 1 as key
:) 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
\ expected output, not standard error of "/opt/sandbox50/bin/run.sh: line
31: 196..."
:) handles lack of argv[1]
So my question is...
My code can't handle the long text of "world, say hello!" It seems it has a segmentation fault when n > 16 (n being the length of a string). Is there a way I can set my memory so that the program can handle a string with more than 16 characters?
Here's my code if it helps:
int main(int argc, string argv[])
{
if (argc != 2)
{
printf("Please enter 2 arguments only\n");
return 1;
}
else
{
string key = argv[1];
int k = atoi(key);
if (k < 0)
{
printf("Please provide a non-negative integer\n");
return 1;
}
else
{
string message = GetString();
int n = strlen(message);
int i = 0;
int m[i], mm[i], crypt[i], c[i], cc[i], cryptic[i];
for (i = 0; i < n; i++)
{
if (isalpha(message[i]))
{
if (isupper(message[i]))
{
m[i] = message[i] - 65;
crypt[i] = ((m[i] + k) % 26);
c[i] = crypt[i] + 65;
printf("%c", c[i]);
}
if (islower(message[i]))
{
mm[i] = message[i] - 97;
cryptic[i] = ((mm[i] + k) % 26);
cc[i] = cryptic[i] + 97;
printf("%c", cc[i]);
}
}
else
{
printf("%c", message[i]);
}
}
printf("\n");
}
}
}