0

I have no idea why my code isnt working for the tests below in Check50.

:( encrypts "XyZ" as "KeD" using NJQSUYBRXMOPFTHZVAWCGILKED as key

:( encrypts all alphabetic characters using DWUSXNPQKEGCZFJBTLYROHIAVM as key **

I cant think of anything wrong except an issue with memory allocation that I'm not familiar with yet? Please let me know if you see anything!

#include <string.h>
#include <math.h>
#include <stdio.h>
#include <cs50.h>
#include <ctype.h>
#include <stdbool.h>


int main(int argc, string argv[])
{
    // setting up key requirments
    // check if E key and length
    if (argc != 2 || strlen(argv[1]) != 26)
    {
        printf("Usage ./substitution key\n");
        return 1;
    }
    else
    {
        for (int i = 0 ; i < strlen(argv[1]) ; i++)
        {
            for (int j = 0; j < strlen(argv[1]); j++)
            {
                // check if key is unique
                if (isalpha(argv[1][i]) == false || (tolower(argv[1][i]) == tolower(argv[1][j]) && i != j ) )
                {
                    printf("Usage ./substitution key\n");
                    return 1;
                }
            }
        }
    }
    
    // prompt user for plain text input
    string plain = get_string("plaintext: ");
    string alpha = "abcdefghijklmnopqrstuvwzyz";
    string key = argv[1];
    int n = strlen(plain);
    char ciph[n];
    //string ciph = plain;  
    
    for (int i = 0; i < n; i++) //going through the plain text index's 
    {
        if (isupper(plain[i]) != 0)
        {
            for (int j = 0; j < strlen(alpha); j++) // going through the alpha index's
            {
                if ( alpha[j] == tolower(plain[i]) )
                {
                    ciph[i] = toupper(key[j]); 
                    break;
                }
            }
        }
        else if (islower(plain[i]) != 0)
        {
            for (int j = 0; j < strlen(alpha); j++) // going through the alpha index's
                {
                    if ( alpha[j] == plain[i] )
                    {
                        ciph[i] = tolower(key[j]);
                        break;
                    }
                }
        }
        else
        {
            ciph[i] = plain[i];
        }
    }
   
    printf("ciphertext: %s\n", ciph);
    return 0;

}
0

If you look carefully at test results, most of them will print correct results followed by extraneous chars. Some look right but are still wrong because they are outputting non-printable chars as well.

The problem is related to ciph[n]. The code does the substitutions and stores them in ciph[], and then prints out ciph. Here's where the problem lies. There is no end of string marker in ciph. In fact, there is no room for it either. So, the program keeps printing chars stored in memory until it stumbles on binary 0x00, or '\0', which it interprets as the EOS marker.

Add 1 to the number of chars in ciph and store the EOS marker in the last position. That will resolve most of your issues.

However, other issues remain that were masked by this problem. I'll leave them for you to find and have a chance to work on. Happy programming! ;-)

If this answers your question, please click on the check mark to accept. Let's keep up on forum maintenance. ;-)

2
  • Hi, thank you for your response! I am trying to work out where the other problems in my code are. Could you give me a hint and verify that the key validation portion of the code (lines 1-32) are not part of the problem? To me, that part seems completely fine but I'd just like to verify with a more trained eye. – catshat Nov 24 '20 at 20:48
  • You should be able to verify that the key validation is good by testing it. Just enter bad keys of all the different types until you're satisfied that it works right. That's part of learning how to test. ;-) As for the other problems, new problems get new questions. Post a new question with your specific questions and issues. – Cliff B Nov 24 '20 at 21:46

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .