EditEdit2: Latest code (working)
int main (int argc, string argv[])
{
// Checking for proper command line input
if (argc != 2)
{
printf("Usage: ./substituion key\n");
return 1;
}
// Checking for 26 character input key
if (strlen(argv[1]) != 26)
{
printf("Key must contain 26 characters.\n");
return 1;
}
// Checking for alphabetic input
for (int i = 0; i < 26; i++)
{
if (argv[1][i] >= 'a' && argv[1][i] <= 'z')
{
argv[1][i] = argv[1][i];
}
else if (argv[1][i] >= 'A' && argv[1][i] <= 'Z')
{
argv[1][i] = argv[1][i];
}
else
{
printf("Key must only contain alphabetic characters.\n");
return 1;
}
}
// Checking for repeated characters
for (int i = 0; i < 26; i++)
{
for (int j = 25; j > i; j--)
{
if (argv[1][i] == argv[1][j])
{
printf("Key must not contain repeated characters.\n");
return 1;
}
}
}
// Removing key's sensitiity
for (int i = 0; i < 26; i++)
{
if (argv[1][i] >= 'A' && argv[1][i] <= 'Z')
{
argv[1][i] = argv[1][i];
}
else
{
argv[1][i] -= 32;
}
}
// Getting plaintext input
string message = get_string("plaintext: ");
// Substitution of plaintext to key
for (int i = 0, n = strlen(message); i < n; i++)
{
for (int j = 0; j < 26; j++)
{
if (message[i] == j + 'A')
{
message[i] = argv[1][j];
j = 26;
}
else if (message [i] == j +'a')
{
message[i] = argv[1][j] + 32;
j = 26;
}
}
}
// Ciphertext
printf("ciphertext: %s\n", message);
return 0;
}
[OUTDATED/ANSWERED] Edit: Here's my code.