0

I'm working on Vigenere.c, and while my code seems to be mostly correct, I keep having very strange outputs which are not even the same every time I run the program (e.g. If I run my code one time with keyword = 'a' and message = 'a' I will get an output different then when I run that same code again also with keyword = 'a' and message = 'a'). Can anyone help me with this? I'm not sure what is going wrong/where to start...

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



int z;
int m;
int found = 0;
int main(int argc, string argv[])
{
if (argc == 2)
{
string keyword = argv[1];


m = strlen(keyword);

  for(z = 0; z < m; z++)
  {
    if (isalpha(keyword[z]))
    {

        continue;
    }

    else
    {
        found = 1;
        break;
    }
  }
     if (argc == 2 && found == 0)
     {



        string message = GetString();
        int n = strlen(message);
        char encrypt[n];
        int j = 0;
        for(int i = 0; i < n; i++)
        {

            if (isalpha(message[i]) && islower(message[i]) &&     islower(keyword[i - j]))
            {
                encrypt[i] = (((message[i] - 'a') + (keyword[(i - j) % m] -     'a')) % 26) + 'a';
            }
            else if (isalpha(message[i]) && islower(message[i]) &&     isupper(keyword[i - j]))
            {
                encrypt[i] = (((message[i] - 'a') + (keyword[(i - j) % m] -     'A')) % 26) + 'a';
            }
            else if (isalpha(message[i]) && isupper(message[i]) &&     islower(keyword[i - j]))
            {
               encrypt[i] = (((message[i] - 'a') + (keyword[(i - j) % m] -     'a')) % 26) + 'A'; 
            }
            else if (isalpha(message[i]) && isupper(message[i]) &&     isupper(keyword[i - j]))
            {
                encrypt[i] = (((message[i] - 'a') + (keyword[(i - j) % m] -     'A')) % 26) + 'A';
            }

            else 
            {
                encrypt[i] = message[i];
                j++;
            }

        }

        printf("%s", encrypt);
     }
     else
     {
         printf("try again");
         return 1;
     }
}
else
{
    printf("error");
    return 1;
}



}

1 Answer 1

3

When encrypt[n] is declared, it doesn't allow for the end of string marker at the end. Also, the end of string marker is never added to the string, so when it is printed at the end of the program, garbage data following the string is also printed, until it finds random data that looks like the end of string marker.

That answers your question, but there are other issues that need to be fixed. But that would be a different question. ;-)

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

2
  • Thanks for the answer, how would I allow for the end of string market at the end? Would it be a simple matter of declaring encrypt[n + 1] instead of encrypt[n] to allow for that extra marker? Jul 28, 2016 at 18:44
  • The "end of string marker" that Cliff B mentioned is the "null character" \0. It marks the end of the string. So your encrypt string, isn't enough to be n characters in length, it has to be n+1, in order to have space for the "null character" too.
    – ChrisG
    Jul 29, 2016 at 14:42

You must log in to answer this question.

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