0

After dealing with caesar using array successfully, I try to do the same with vinegere using this code

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

string encrypt(string s);

int main(int argc, string argv[])
{
    if(argc != 2)
    {
        printf("Usage: ./vigenere k");
        return 1;
    }


    //make an array of 26 uppercase char
    int i = 0;                                                          
    int char_upper[26] = {0};
    for(char c = 'A'; c <= 'Z'; c++)
    {
        char_upper[i] = c;
        i++;
    }

    //make an array of 26 lowercase char
    i = 0;                                                              
    int char_lower[26] = {0};
    for(char c = 'a'; c <= 'z'; c++)
    {
        char_lower[i] = c;
        i++;
    }

    //make arguement string into key array
    int n4 = strlen(argv[1]);
    int key[n4];
    for (int i4 = 0; i4 < n4; i4++)
    {
        if(!isalpha(argv[1][i4]))
        {
            printf("error\n");
            return 1;
        }

        int i5 = 0;
        if(argv[1][i4] == toupper(argv[1][i4]))
        {
            while(argv[1][i4] != char_upper[i5])
            {
                i5++;
            }
            key[i4] = i5;
        }

        else if(argv[1][i4] == tolower(argv[1][i4]))
        {
            while(argv[1][i4] != char_lower[i5])
            {
                i5++;
            }
            key[i4] = i5;
        }
    }

    //get input and create output string
    printf("plaintext: ");                                              
    string input = get_string();
    int n1 = strlen(input);
    char output[n1];
    int count = 0;
    for(int i1 = 0; i1 < n1; i1++)
    {
        int i2 = 0;

        if(isalpha(input[i1]))
        {
            //loop until get same uppercase char
            if(input[i1] == toupper(input[i1]))
            {
                while(input[i1] != char_upper[i2])                 
                {
                    i2++;
                }

                i2 = (i2 + key[count % n4]) % 26;
                output[i1] = char_upper[i2];
            }

            //loop until get same lowercase char
            else if(input[i1] == tolower(input[i1]))
            {
                while(input[i1] != char_lower[i2])                    
                {
                    i2++;
                }

                i2 = (i2 + key[count % n4]) % 26;
                output[i1] = char_lower[i2];
            }
            count++;
        }

        //keep unchange char in output array
        else
        {
            output[i1] = input[i1];                                     
        }
    }
    printf("ciphertext: %s\n", output);
}

When I check the program it actually worked as intended, but with check50 I got these message:

:( encrypts "a" as "a" using "a" as keyword
   \ expected output, but not "ciphertext: a?Åÿ\n"
:) encrypts "barfoo" as "caqgon" using "baz" as keyword
:) encrypts "BaRFoo" as "CaQGon" using "BaZ" as keyword
:) encrypts "BARFOO" as "CAQGON" using "BAZ" as keyword
:( encrypts "world!$?" as "xoqmd!$?" using "baz" as keyword
   \ expected output, but not "ciphertext: xoqmd!$?)\f@\n"
:( encrypts "world, say hello!" as "xoqmd, rby gflkp!" using "baz" as keyword
   \ expected output, but not "ciphertext: xoqmd, rby gflkp!§öÿ\n"
:) handles lack of argv[1]
:) handles argc > 2
:) rejects "Hax0r2" as keyword

which suggest that there is an addition memory slot being accessed unknowingly. The problem isn't consistent and all the array length should be appropriate so I don't really know how to debug this

0

Your string needs to be null terminated - that is, it has to have '\0' at the end so that printf and other functions know where it ends. To accomplish this, you will also need to make our output array bigger, so that it has room for this extra character.

3
  • It does seem like the problem indeed lies with the number of elements in the output array. I tried output[n+1], output[n+10] but got the same errors. Thing only changes when I went output[100] at which point only encrypts "world!$?" as "xoqmd!$?" persists. Funny thing is then I tried output[1000] then another error cameback. This array thing is pretty strange – Huynq Apr 30 '17 at 16:28
  • Increasing the size of the array, on its own, will not solve the issue with null termination. You still have to make sure that, after you have added all the characters that belong in the string, you also add a '\0' to show that the end of the string has been reached. – Levi Roth Apr 30 '17 at 20:35
  • Ah I see. That solved it. Thanks – Huynq May 1 '17 at 1:51

You must log in to answer this question.

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