check50 results in all the tests passing but also produces a repetitive error, Invalid key. Please provide a positive integer. Cause Timed out while waiting for program to exit. Not sure which line is the culprit. Thank you
#include <ctype.h>
#include <stdio.h>
// Function to encrypt a character using Caesar's cipher
char caesarEncrypt(char ch, int key)
{
if (isalpha(ch))
{
char base = islower(ch) ? 'a' : 'A';
return (ch - base + key) % 26 + base;
}
return ch; // Preserve non-alphabet characters
}
int main()
{
int key;
// Prompt the user for the encryption key
printf("Enter the encryption key (a positive integer): ");
if (scanf("%d", &key) != 1 || key <= 0)
{
printf("Invalid key. Please provide a positive integer.\n");
return 1;
}
// Consume the newline character left in the input buffer
getchar();
// Input message from the user
printf("Enter a message to encrypt: ");
char message[1000];
fgets(message, sizeof(message), stdin);
// Encrypt the message
printf("Encrypted message: ");
for (int i = 0; message[i] != '\0'; i++)
{
printf("%c", caesarEncrypt(message[i], key));
}
printf("\n");
return 0;
}