0

Sorry for my English( I am learning. 1.I have make this code, but when I have wrote "isduplicate" function it makes uncatchable bug. Code work as my expectetion only in debugger. So where is it? Function "isduplicate" return 0 in debugger and probably 1 in console. Console screen uncatchable bug

2.Is it correct way to find double value? '''

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

string code(string text, string key);
int isduplicate(string c);

int main(int argc, string argv[])
{
    if ( argc != 2 )
    {
        printf("Usage: ./substitution key\n");
        return 1;    
    }
    int l = strlen(argv[1]);
    for (int i = 0; i < l; i++)
    {
        if ( argv[1][i] < 'A' || (argv[1][i] > 'Z' && argv[1][i] < 'a') || argv[1][i] > 'z' )
        {
            printf("Usage: ./substitution key\n");
            return 1;
        }
    }
        if ( l < 26 )
        {
            printf("Key must contain 26 characters.\n");
            return 1;
        }
        if (isduplicate(argv[1]))
        {
            printf("Key must contain each letter exactly once.\n");
            return 1;
        }
        string plaintext = get_string("plaintext: ");
        printf("ciphertext: %s\n", code(plaintext, argv[1]));
}

string code(string text, string key)
{
    for (int i = 0; i < strlen(text); i++)
    {
        if (text[i] >= 'A' && text[i] <= 'Z')
        {
            int u = text[i] - 65;
            text[i] = toupper(key[u]);
        }
        else if (text[i] >= 'a' && text[i] <= 'z')
        {
            int l = text[i] - 97;
            text[i] = tolower(key[l]);
            
        }
    }
    return text;
}

int isduplicate(string c)
{
    int V[26];
     for (int i = 0; i < 26; i++ )
    {
       V[i] = 0;
    }
    int Number[26];
    {
        for (int i = 0; i < 26; i++)
        {
           Number[i] = toupper(c[i]) - 64;
        }
    }
    for (int i = 0; i < 26; i++ )
    {
        if (V[Number[i]] == 0)
        {
            V[Number[i]] = 1;
        }
        else 
        {
            return 1;
        }
    }
    return 0;
}

1 Answer 1

1

Sometimes, an error or unpredictable condition can be masked or hidden by the debugger when you run the code in the debugger. This is the case here. I cannot tell you why it is failing, only that it does. When this happens, you need to start getting creative and try to more carefully follow what's happening to find the bug.

The error here is very subtle. Look at the following code:

       Number[i] = toupper(c[i]) - 64;

This is intended to convert letters first to uppercase and then to numbers from 0 to 25. Unfortunately, there's an off-by-one error. It's converting the letters to numbers in the range from 1 to 26. When a z is processed, the results are unpredictable. It's probably stepping on whatever is stored in the next 8 bytes of memory physically stored after the Number[] array.

In any case, this is the problem. I'll leave it to you to investigate exactly what's happening. ;-)

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

2
  • Thank you very much. It's very simple mistake. But I was not too concentrate to catch that. Now I clearly understood what is happening.
    – pawa309
    Commented Oct 16, 2020 at 8:53
  • Don't feel bad. ;-) Bugs are often something little like that - something really subtle that you can look at a thousand times and not realize it's even there. Happens all the time.
    – Cliff B
    Commented Oct 16, 2020 at 16:10

You must log in to answer this question.

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