0
#include <cs50.h>
#include <string.h>
#include <stdlib.h>
#include <ctype.h>
#include <stdio.h>

int main(int argc, string argv[])
{
    // Checks if there are two arguments.
    if (argc != 2)
    {
        printf("Please input key.\n");
        return 1;
    }

    // Checks if the key is only made of letters and has no repetitions.
    for (int i = 0; argv[1][i] != '\0'; i++)
    {
        for (int j = 0; j < i; j++)
        {
            if (argv[1][j] == argv[1][i])
            {
                printf("Key must have 26 unique letters.\n");
                return 1;
            }
        }
        if (isdigit(argv[1][i]) != 0)
        {
            printf("Please input proper key.\n");
            return 1;
        }
        if (!isalpha(argv[1][i]))
        {
            printf("Key must only contain letters.\n");
            return 1;
        }
        if (strlen(argv[1]) != 26)
        {
            printf("Key must have 26 letters.\n");
            return 1;
        }
    }


    char alphabet[2][26];
    for (int j = 0; j < strlen(argv[1]); j++)
    {
        alphabet[0][j] = 'A' + j;
        alphabet[1][j] = argv[1][j];
    }
    // Gets input from user.
    string text = get_string("Plaintext: ");

    // Variable that stores the encrypted letter.
    char ciphertext[strlen(alphabet[0])];

    // Encrypting input from user.
    for (int i = 0; i < strlen(text); i++)
    {
        if (isupper(text[i]))
        {
            for (int j = 0; j < strlen(argv[1]); j++)
            {
                if (alphabet[0][j] == text[i])
                {
                    ciphertext[i] = toupper(alphabet[1][j]);
                }
            }
        }
        if (islower(text[i]))
        {
            for (int j = 0; j < strlen(argv[1]); j++)
            {
                if (tolower(alphabet[0][j]) == text[i])
                {
                    ciphertext[i] = tolower(alphabet[1][j]);
                }
            }
        }
        else if (!isalpha(text[i]))
        {
            ciphertext[i] = text[i];
        }
    }

    printf ("ciphertext: %s\n",  ciphertext);
}

That's my code and it works pretty well, until I encrypt 35-36 character sentences or phrases with the last one being a period. It adds a letter or a non-ASCII character to the output. My question is, what's wrong with it?

1 Answer 1

2

Did you remember to add one extra char to the length of ciphertext and to insert the end of string marker in that last space?

Also, shouldn't the length of ciphertext be based on the length of string and not on the length of the key?

coding tip: Whenever you use the length of a string as the control of a for loop, set a var equal to that length in the setup clause. If a char in the string gets changed to '\0', the recheck on each loop will interpret the new char as the end of string marker and short circuit the for loop. Using an extra var prevents this from happening. Here's how:

for (int i = 0, n = strlen(text); i < n; i++)

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

4
  • Okay just tried it but still no dice. Honestly, I'm really confused about this.
    – Chi Go
    Commented Dec 31, 2022 at 11:42
  • what changes did you make, exactly?
    – Cliff B
    Commented Dec 31, 2022 at 18:05
  • I tried this: for (int i = 0, n = strlen(text); i < n; i++) but it still adds an unneccessary character.
    – Chi Go
    Commented Jan 1, 2023 at 4:23
  • Did you fix the length of the ciphertext array and insert the EOS marker at the end?
    – Cliff B
    Commented Jan 1, 2023 at 13:45

You must log in to answer this question.

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