0

I am getting some garbage values when declaring my array. I kinda understand that when declaring without initializing it provides garbage values, but I don't understand why the array is larger than what I set it to. The issue happens when I test with

./vigenere a

plaintext: a

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

#define ABC 26 // sets the alphabet length

// Function to ensure input is a string
bool alpha_check(string text)
{
    for (int i = 0, n = strlen(text); i < n; i++)
    {
        if (!isalpha(text[i]))
        {
            return false;
        }
    }
    return true;
}

// Main function
int main(int argc, string argv[])
{
    // Ensures single command-line argument and string
    string k = argv[1];
    if (argc != 2 || alpha_check(k) == false)
    {
        printf("Usage: ./vigenere k\n");
        return 1;
    }
    string plain = get_string("plaintext: ");
    int len_plain = strlen(plain);
    int len_k = strlen(k);
    //printf("%i\n", len_plain);
    char cipher[len_plain];
    //eprintf("%s\n", cipher);

    // Turn plaintext into ciphertext
    int c = 0; // Counter for each use of key
    int h; // Index key
    int key;
    for (int i = 0; i < len_plain; i++)
    {
        h = c % len_k;
        key = (isupper(k[h])) ? k[h] - 'A' : k[h] - 'a';
        if (isalpha(plain[i]) && isupper(plain[i])) // Upper letter
        {
            cipher[i] = (plain[i] - 'A' + key) % ABC + 'A';
            c++;
        }
        else if (isalpha(plain[i]) && islower(plain[i])) // Lower letter
        {
            cipher[i] = (plain[i] - 'a' + key) % ABC + 'a';
            c++;
        }
        else
        {
            cipher[i] = plain[i];
        }
    }
    printf("ciphertext: %s\n", cipher);
    return 0;
}

When I use debug50 to run through the code, it seems that the cipher array is larger than it's supposed to be. len_plain = 1, so cipher should be 1 element, but it seems to not be the case.

array error

Also I don't understand why if I 'uncomment' line 34, the printf before char cipher[len_plain], then it doesn't give me the issue. The same applies if I remove the function I have before the main function.

1

Did you remember to store the end of string marker in cipher? Did you allow space to store it?

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

2
  • Thought I had tried that, guess I had but incorrectly. So what I changed was: Char cipher[len_plain + 1]; And at the end just before it prints the cipher: cipher[len_plain] = '\0'; Do you always need to add that 'end of string marker' when creating a new array? Somehow I didn't have to do that in the caesar, or at least it didn't give me errors in the same way (maybe I was lucky). – nBock Nov 20 '18 at 7:57
  • A string, i.e., a char array should always have the end of string marker. Many functions, like strlen and printf depend on finding it. If you printed the chars one by one, then it wouldn't be needed, but it's still a good idea to have the safety check. – Cliff B Nov 20 '18 at 8:09

You must log in to answer this question.

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