0

My code compiles and I tested it with the example "Meet me at the park at eleven am", the result was "Negh zf av huf pcfx bt gzrwep oz", and that is correct. But when I run "check50 cs50/2018/x/vigenere" command I get different results... My code is little bit messy right now, I apologize for that.

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

int main(int argc, string argv[])
{

if (argv[1] != 0)
{
// Getting the command line argument to use it like a key
string key = argv[1];

// Count the entered no letter symbols
int count_non_letters = 0;

for (int i = 0; i < strlen(key); i++)
{
    if (key[i] >= 48 && key[i] <= 57)
    {
        count_non_letters++;
    }
}
// Checking if the user inputs command line argument, if so, the program runs
if ((argc < 3) && (count_non_letters == 0))
{

    // The index number of the keytext array
    int key_index;

    // The index is based on the length of the alphabet
    char alpha_index[26];

    // Checking if the entered symbol is alphabet
    bool is_alpha = false;

    // If the plain text is larger than the crypt text we loop over and over again the letters in the crypt text to match the plain text size
    int crypt_text_loop;

    // The index number of the array of string
    int index;

    // Simple count to help for the array filling
    int count = 0;

    // The formula to cript the text
    int crypt = 0;

    // Counting the alphabetical symbols in the plain text
    int count_alpha = 0;

    // Asking the user to enter the plain text
    string plain_text = get_string("Enter text: ");

    for (int i = 'A'; i <= 'Z'; i++)
    {
        alpha_index[count] = i;
        count++;
    }

    // Checking the entered text and encrypting it
    printf("ciphertext: ");

    // Checking every symbol of the entered text
    for (int i = 0; i < strlen(plain_text); i++)
    {

        // Checking if the character is a letter
        if ((plain_text[i] >= 65 && plain_text[i] <= 90) || (plain_text[i] >= 97 && plain_text[i] <= 122))
        {
            is_alpha = true;

            index = toupper(plain_text[i]) - alpha_index[0];

            crypt_text_loop = alpha_index[count_alpha] % strlen(key);

            key_index = toupper(key[crypt_text_loop]) - alpha_index[0];

            crypt = (index + key_index) % 26;

            count_alpha++;

            // Checking if the character is a capital letter
            if (isupper(plain_text[i]) && is_alpha)
            {
                printf("%c", alpha_index[crypt]);
            }

            // Checking if the character is a small letter
            if (islower(plain_text[i]) && is_alpha)
            {
                printf("%c", tolower(alpha_index[crypt]));
            }
        }

        // If is not a letter, then is a symbol or space and we print it like it is
        else
        {
            printf("%c", plain_text[i]);
        }
    }

    printf("\n");
}
else
{
    return 1;
}
}
else
{
  return 1;
}
}

1 Answer 1

1

This looks wrong:

crypt_text_loop = alpha_index[count_alpha] % strlen(key);

Did you mean

crypt_text_loop = alpha_index[count_alpha % strlen(key)];

?

edit >>>>>>

I was correct in that the % strlen(key) belongs to the count_alpha. Turns out the whole alpha_index makes no sense.

            crypt_text_loop = alpha_index[count_alpha] % strlen(key);

            key_index = toupper(key[crypt_text_loop]) - alpha_index[0];

should be

            key_index = toupper(key[count_alpha % strlen(key)]) - 'A';

edit <<<<<<

The condition in

        if (key[i] >= 48 && key[i] <= 57)

matches digits only. For non-alphabetic characters, you could use isalpha, like

        if (!isalpha(key[i]))

Counting the non-alphabetic characters is somewhat useless. Once you find the first user mistake, you could just print a usage reminder and return, that would save you some nesting layers and elses. Same applies to the argc checks. If it isn't exactly 2, tell the user they messed up and exit.

2
  • I use "count_alpha" for the purpose of making the keyword go together with the plain text and when is a space for example did not encrypt it. For the second suggestion You are right. I take note. But the rare point is that my code gets the same result like the example given and then with the test of submission there is nor one match.... Dec 7, 2018 at 19:13
  • Your code will break on text longer than 26 characters. I am somewhat surprised it works at all. I added an edit with a suggested change. BTW, the specified prompt for input is plaintext:, but is never tested for.
    – Blauelf
    Dec 9, 2018 at 9:43

You must log in to answer this question.

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