I just finished my code for hail Caesar but for some reason when I don't give it a key, instead of exiting the programme and printing it gives me a segmentation error. Also It fails check 50 saying that I encrypt the text wrong but it works correct except the issue with the no key situation.
Any help would be much appreciated, thanks in advance.
#include <cs50.h>
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <ctype.h>
int lower(int input);
int upper(int input);
int key;
int main(int argc, string argv[])
{
key = atoi(argv[1]);
if (argc != 2)
{
printf("./caesar key (the key must be an int)");
return 1;
}
else
{
printf("Your Key is:%i\nPlease provide me with the plain text my kind sir!:", key);
string plain = GetString();
for (int i = 0, n = strlen(plain); i < n; i++)
{
if (isalpha(plain[i]))
{
if (isupper(plain[i]))
{
printf("%c", upper(plain[i]));
}
if (islower(plain[i]))
{
printf("%c", lower(plain[i]));
}
}
else
{
printf("%c", plain[i]);
}
}
}
}
int upper(int input)
{
int i = input - 65 + key;
i = i % 26;
int k = i + 65;
return k;
}
int lower(int input)
{
int i = input - 97 + key;
i = i % 26;
int k = i + 97;
return k;
}