0

I have below code written and majority of code is working, but when do check CS50,

I have additional output if crypt "a" using k=1

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

int main(int argc, string argv[])
{
     // Get the key
    if (argc != 2)
    {
        printf("Usage: ./caesar key\n");
        return 1;
    }
    // Check if arg[1] is a digit number
    char *k = argv[1];
    int l = strlen(k);

    for (int i = 0; i < l; i++)
    {
        if (isdigit(k[i]) == 0)
        {
            printf("Usage: ./caesar key\n");
            return 1;
        }
    }
    int key = atoi(argv[1]);

    // Get plaintext from user
    string text = get_string("plaintext: ");
    int itext = strlen(text);

    // Cryp the plaintext
    char cryp[itext];
    for (int j = 0; j < itext; j++)
    {
        if (isalpha(text[j]))
        {
            if (isupper(text[j]))
            {
                cryp[j] = (((text[j] - 65 + key ) % 26) + 65);
            }
            else if (islower(text[j]))
            {
                cryp[j] = (((text[j] - 97 + key ) % 26) + 97);
            }
        }
        else
        {
              cryp[j] = text[j];
        }
    }
    printf("ciphertext: %s\n", cryp);
    return 0;
}

1 Answer 1

4

It's a common newbie problem. The code "encodes" the phrase into the char array cryp[] one letter at a time, which is fine. BUT, there's something missing - the end of string marker, \0. The code didn't allocate space for it or insert it at the end of the string.

A string or char array should always have one extra character at the end, the EOS marker, \0, or 0x00 in hex. This is a single byte containing all binary zeros. It marks the end of a string. A number of library functions, particularly printf() depend on this marker.

The printf() function will print text starting at the given memory address until it encounters the EOF marker. If the marker isn't there, as is the case here, it will keep printing everything it finds in memory, treating the data as ASCII characters, until it encounters a random byte that is all zeros. This is what you're seeing.

The fix is simple. Add 1 to the length of cryp[] when you declare it and insert the EOF marker in the last element of the array.

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

5
  • Thanks, I am still not sure exactly how to do it, but I got the idea, let me do some research and see if I can get around it. Commented Nov 4, 2022 at 13:45
  • Increased my scry string counter to +1, and it works perfectly and submitted the code successfully, thanks a lot for your help. Commented Nov 4, 2022 at 14:10
  • Did you remember to explicitly insert "\0" in the last position? That guarantees that it's properly set. Otherwise you could be relying on a default initialization, which, under certain conditions wouldn't occur. (usually involves malloc). Best to develop that habit now.
    – Cliff B
    Commented Nov 4, 2022 at 20:26
  • @CliffB almost done pset2 and accidentally stumbled upon this. I have quity the same solution, so the question is - how to insert "/0" to the last position? Using while loop?
    – lkchek
    Commented Nov 6, 2022 at 19:57
  • mystring[x] = '\0' ; where "x" is the number of the last position.
    – Cliff B
    Commented Nov 6, 2022 at 21:56

You must log in to answer this question.

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