Reputation: 1
I can't see what my code for pset2 substitution is missing. When I use check50 to test the program it returns this result:
:) substitution.c exists
:) substitution.c compiles
:( encrypts "A" as "Z" using ZYXWVUTSRQPONMLKJIHGFEDCBA as key
expected "ciphertext: Z\...", not "ciphertext: Z\..."
:( encrypts "a" as "z" using ZYXWVUTSRQPONMLKJIHGFEDCBA as key
expected "ciphertext: z\...", not "ciphertext: z\..."
:( encrypts "ABC" as "NJQ" using NJQSUYBRXMOPFTHZVAWCGILKED as key
expected "ciphertext: NJ...", not "ciphertext: NJ..."
:( encrypts "XyZ" as "KeD" using NJQSUYBRXMOPFTHZVAWCGILKED as key
expected "ciphertext: Ke...", not "ciphertext: Ke..."
:) encrypts "This is CS50" as "Cbah ah KH50" using YUKFRNLBAVMWZTEOGXHCIPJSQD as key
:) encrypts "This is CS50" as "Cbah ah KH50" using yukfrnlbavmwzteogxhcipjsqd as key
:) encrypts "This is CS50" as "Cbah ah KH50" using YUKFRNLBAVMWZteogxhcipjsqd as key
:( encrypts all alphabetic characters using DWUSXNPQKEGCZFJBTLYROHIAVM as key
expected "ciphertext: Rq...", not "ciphertext: Rq..."
:) handles lack of key
:) handles invalid key length
:) handles invalid characters in key
:) handles duplicate characters in key
:) handles multiple duplicate characters in key
my code is :
#include <stdio.h>
#include <cs50.h>
#include <stdlib.h>
#include <ctype.h>
#include <string.h>
int main(int argc, string argv[])
{
// VALIDATE THE KEY
// for argc
if (argc != 2)
{
printf("Usage: ./substitution key\n");
return 1;
}
// var n for nubers of argv
int n = 26;
// for alphabit
for (int i = 0; i < n; i++)
{
if (isalpha(argv[1][i]) == 0)
{
printf("Usage: ./substitution key\n");
return 1;
}
}
// for length of argv
if (n != 26)
{
printf("Key must contain 26 characters.\n");
return 1;
}
// repeated charcters
for (int i = 0; i < n; i++)
{
for (int j = 0; j < n; j++)
{
// step over for the character
if (i == j)
{
j += 1;
}
// error message for repeted character
if (argv[1][i] == argv[1][j])
{
printf("Key must not contain repeated characters.\n");
return 1;
}
}
}
// GET PLAINTEXT
string plaintext = get_string("plaintext: ");
int npi = strlen(plaintext);
// ENCIPHER AND PRINT CIPHERTEXT
// var for ciphertext
char ci[npi];
// print ciphertext
printf("ciphertext: ");
// encipher
for (int i = 0; i < npi;)
{
for (int j = 0; j < n; j++)
{
// for not alphabits
if (isalpha(plaintext[i]) == 0)
{
printf("%c", plaintext[i]);
i++;
j = 26;
}
// for upper case
if (isupper(plaintext[i]) && ('A' + j) == plaintext[i])
{
ci[i] = toupper(argv[1][j]);
printf("%c", ci[i]);
i++;
j = 0;
}
// for lower case
else if (islower(plaintext[i]) && ('a' + j) == plaintext[i])
{
ci[i] = tolower(argv[1][j]);
printf("%c", ci[i]);
i++;
j = 0;
}
}
}
printf("\n");
return 0;
}
Upvotes: 0
Views: 155
Reputation: 6520
Program outputs non printable characters, which is why the check50 output is confusing, it is not "printing" them.
Try this at the command line and enter a as the plaintext to see result:
./substitution ZYXWVUTSRQPONMLKJIHGFEDCBA | cat --show-nonprinting
The fundamental flaw in the program is changing the loop variable values within the loops (i
and j
). In the plaintext "a" example, i
gets greater than npi
. Walk it through on paper.
Upvotes: 1