Here is my code-
#include <stdio.h>
#include <cs50.h>
#include <stdlib.h>
#include <ctype.h>
#include <string.h>
bool check_distinct_char(string s);
bool check_char(string s);
int main(int argc, string argv[])
{
if (argc == 2 && check_distinct_char(argv[1]))
{
int n = strlen(argv[1]);
if (n == 26 && !check_char(argv[1]))
{
string plain = get_string("plaintext: ");
printf("ciphertext: ");
for (int j = 0, len = strlen(plain); j < len; j++)
{
if (isupper(plain[j]))
{
printf("%c", toupper(argv[1][((int)plain[j] - 65) % 26]));
}
else if (islower(plain[j]))
{
printf("%c", tolower(argv[1][((int)plain[j] - 97) % 26]));
}
else
{
printf("%c", plain[j]);
}
}
printf("\n");
return 0;
}
else
{
printf("Key must contain 26 characters.");
return 1;
}
}
else if (!check_distinct_char(argv[1]))
{
return 1;
}
else
{
printf("Usage: ./substitution key");
return 1;
}
}
bool check_char(string s)
{
for (int i = 0, len1 = strlen(s); i < len1 ; i++)
if (isdigit(s[i]))
{
return 1;
}
return 0;
}
bool check_distinct_char(string s)
{
for (int j = 0, len = strlen(s); j < len ; j++)
{
for (int k = 0; k < len ; k++)
{
if (s[j] == s[k])
{
return 1;
}
}
}
return 0;
}
I think it should work as there is a return value incase the letters are repeated in the string.