0

When I use debug50 on this program I get 26 as the value of 'a' but when I run it I get random large values of 'a'.

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

int main(int argc, string argv[])
{
    char k1[26];
    char k2[26];
    int a;
    string s = argv[1];
    
    for (int i = 0, n = 26; i < n; i++)
    {
        k1[i] = toupper(s[i]);
        k2[i] = tolower(s[i]);
    }
    
    printf("%lu\n",strlen(k1));
    printf("%s\n",k1);
    printf("%s\n",k2);
    printf("%s\n",s);
    
    for (int i = 0, n = 26; i < n; i++)
    {
        for (int j = 0, n1 = 26; j < n1; j++)
        {
            
            if (s[i] == k1[j] || s[i] == k2[j])
            {
                 a = a + 1;
            }
            
        }
    }
    printf("%i\n",a);
    if (a > 26)
    {
        printf("repeat\n");
    }
    else
    {
        printf("not repeat\n");
    }
}

when I use debug50

~/ $ debug50 try VCHPRZGJNTLSKFBDQWAXEUYMOI 
27
VCHPRZGJNTLSKFBDQWAXEUYMOI@
vchprzgjntlskfbdqwaxeuymoi@
VCHPRZGJNTLSKFBDQWAXEUYMOI
26
not repeat

But when I run the code. I get the following output:

~/ $ ./try VCHPRZGJNTLSKFBDQWAXEUYMOI                                                                                                                                                           
27
VCHPRZGJNTLSKFBDQWAXEUYMOI@
vchprzgjntlskfbdqwaxeuymoi@
VCHPRZGJNTLSKFBDQWAXEUYMOI
32534
repeat

The value of 'a' changes every time I run the code. Why is this happening and how do I solve it? Also, what is the reason that string k1 and k2 has '@' at the end?

1
  • Have you tried using other keys, or changing the case?
    – Vsjain
    Oct 21, 2020 at 16:40

1 Answer 1

0

First, you need to understand that when running a program inside a debugger, memory allocation can be different than when running a program itself. That can lead to slightly different behaviors and to different results. The debuggers can sometimes mask a bug in code. It's not an ideal situation, but as long as you know it, you can learn to recognize when something is amiss.

Now, on to the code. There are two minor issues with the code. First, the variable a is not initialized. The code should set it to zero when it is created. Never depend on "default" initialization. In C, some var types are initialized by default, but many are not. It's just better to not depend on any. I tested the code with a initialized to 0 and it worked both consistently and correctly for me.

Now, about those stray @ being printed. Many string related functions, including printf and strlen, depend on the existence of an end of string marker, \0, to actually exist at the end of the string. Without it, they will continue to either count or print characters until they actually find a zero byte in the physical memory. That's what's happening here.

The k1 and k2 arrays are being treated as strings elsewhere in the code, but they weren't constructed to account for this. To fix this issue, allocate one more element (27 instead of 26) and initialize the last element with the EOS marker, \0.

That should clean up most of the issues. Not sure if there are any others, but that would be for another question. ;-)

One side note. Using single letter variable names for anything other than for loop counters is a very bad practice. You should get in the habit now of using descriptive var names. They don't have to be very long or excessively descriptive, but 1 and 2 char var names are usually bad. THink about this. You've written 200 lines of code and have a problem with a variable. Now, you want to search for every occurrence of that var. What do you think happens when you do a search on "a" vs. a search on "count"? You can test that by doing those searches on this page and see how many results you get for each. ;-)

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

You must log in to answer this question.

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