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?