The below code lost a mark; check50 gave the explanation just below. A different time-out query (by Abhijit) appears to have had a different cause. The check50 line used: check50 cs50/problems/2021/x/substitution Check50 gave this explanation:
:( handles invalid characters in key
Cause
timed out while waiting for program to exit
Log
running ./substitution ZWGKPMJRYISHFEXQON2DLUACVT...**
Lines 47-50 below (i.e. // error if cipher contains non-alpha value) aim to handle non-alphabetic chars. Also, while running checks, the substitution key (above) that caused the check50 error was picked up another way--it has fewer than 26 chars. I'm still unsure why check50 'timed out'.
Any help (or other improvements) with the below would be much appreciated--thanks in advance!
#include <cs50.h>
#include <stdio.h>
#include <ctype.h>
#include <string.h>
#include <stdlib.h>
const int LEN = 26;
int alphaix[LEN];
char alpha[LEN];
int main(int argc, string argv[])
{
string err = "Input error: ";
if (argc == 2)
{
string cipher = argv[1];
int ciphlen = strlen(cipher);
// check ciphlen against LEN
if (ciphlen == LEN)
{
for (int i = 0; i < LEN; i++)
{
// assign cipher[i] to temp
char temp = cipher[i];
// check if cipher char is alpha
if (isalpha(temp))
{
// populate alpha arr with cipher lower chars
if (islower(temp))
{
// alphaix is a diagnostic var only
alpha[i] = temp;
alphaix[i] = temp - 97;
}
// populate alpha arr with cipher upper chars
else
{
alpha[i] = temp;
alphaix[i] = temp - 65;
}
}
// error if cipher contains non-alpha value
else
{
printf("%s cipher contains non-alpha value\n", err);
**// insertion of this single line below fixed the error described in this question!**
**Return 1;**
}
}
}
// if ciphlen != LEN
else
{
printf("%s must contain 26 chars\n", err);
return 1;
}
// ciphertext duplicate check
for (int i = 0; i < LEN; i++)
{
char temp = toupper(cipher[i]);
for (int j = (i + 1); j <= LEN; j++)
{
char temp2 = toupper(cipher[j]);
if (temp2 == temp)
{
printf("%s %c, %c duplicate cipher char\n", err, temp, temp2);
return 1;
}
}
}
}
// if too few/many command-line arguments
else
{
printf("%s c-line arg\n", err);
return 1;
}
/*
// diagnostic only, print alpha, alpha index and array
for (int k = 0; k < LEN; k++)
{
printf("alpha[%i], %c: %i; ",k, alpha[k], alphaix[k]);
}
printf("\n");
*/
// collect plaintext from user
string plaintxt = get_string("plaintext: ");
int plen = strlen(plaintxt);
int tempix = 0;
printf("ciphertext: ");
for (int i = 0; i < plen; i++)
{
// assign plaintxt[i] to temp
char temp = plaintxt[i];
// check temp is alpha
if (isalpha(temp))
{
// if temp is lower
if (islower(temp))
{
// calculate temp index
tempix = temp - 97;
for (int j = 0; j < LEN; j++)
{
// compare temp index to alpha array
if (tempix == alphaix[j])
{
temp = tolower(alpha[tempix]);
}
}
// print ASCII char
printf("%c", temp);
}
// if temp char is upper
else
{
tempix = temp - 65;
for (int j = 0; j < LEN; j++)
{
if (tempix == alphaix[j])
{
temp = toupper(alpha[tempix]);
}
}
printf("%c", temp);
}
}
// if temp char is non-alpha
else
{
printf("%c", temp);
}
}
printf("\n");
}