0

This is my code:-

#include<iostream>
#include<string>
using namespace std;

int main()
{
    string magazine="aab";
    string ransomNote="aa";
    int hash[123]={};
        int i;
        for(i=0;i<=magazine.length();i++)
        {
            hash[magazine[i]]++;
        }
        for(i=0;i<123;i++)
        {
            cout<<hash[i]<<" ";
        }
        cout<<endl;
}

The output is:-

1 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 2 1 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0

Prophet_17
  • 35
  • 2
  • 2
    for(i=0;i<=magazine.length();i++) its because of the <= part of this for loop. – drescherjm Feb 06 '22 at 18:13
  • 2
    The string is 0-terminated and since you read past the last char, you read the terminating 0 on the last loop iteration. – fabian Feb 06 '22 at 18:13
  • `for(i=0;i<=magazine.length();i++) { hash[magazine[i]]++; }` could be `for(char mag:magazine) { hash[mag]++; }` If the compiler messes up the indexing, you'll have an interesting story to tell. To other programmers, at any rate. – user4581301 Feb 06 '22 at 18:28
  • 2
    `int hash[123]={};` and `for(i=0;i<123;i++)` avoid the [Magic Number](https://stackoverflow.com/questions/47882/what-is-a-magic-number-and-why-is-it-bad). It sucks to change one and forget to change the other. – user4581301 Feb 06 '22 at 18:30

1 Answers1

0

In c++ chars are almost the same thing as integers and they are stored as integers. So when you are performing hash[magazine[i]]++; what really happens is that the magazine[3] (which is the NULL character) will be promoted to an int (Source: cpp reference: Implicit Conversions, the equivalent of which is 0 based on the cpp reference: ASCII codes.

Thus, the statement hash[3] = 0 is true. That said, the below code which is similar to the code used in the example

string x = hash[magazine[3]]++; //first x is assigned with the value of hash[magazine[3]], then hash[magazine[3]] is incremented.
cout << "output: " << x << "\n";

will produce output: 0.

That happens because the hash[0] is assigned with the value of hash[magazine[3]] before the increment operator.Equivalently, the increment (++ operator) will be evaluated after the rest of the expression is -- after the assignment has been done.

If you want to avoid this behavior, you can do the below:

string x = ++hash[magazine[3]]; //first hash[magazine[0]] is incremented, then it is assigned to x.
cout << "output: " << x << "\n";

which will produce output: 1.

However, hash[magazine[i]]++ is inside a loop. This means that after the loop concludes, the expression has already been evaluated and the hash array contains the new values, thus, hash[0] will be equal to 1.

Cheers,

  • "what really happens is that the magazine[0] = 'a' (which is a char) will be promoted to an int (Source: cpp reference: Implicit Conversions, the equivalent of which is 0 based on the cpp reference: ASCII codes." . I get that. But the int value of 'a' is 97 ( according to ASCII). How is the int equivalent of 'a' zero(0) ? – Prophet_17 Feb 08 '22 at 11:57
  • @ShwetanshSharma updated my answer. In c++ empty array initializations will also initialize elements as 0. However, the correction that has been made is that when the loop reaches magazine[3], will read the NULL character which is equivalent to 0, thus hash[0] will be 1. – Ioannis Brant-Ioannidis Feb 08 '22 at 20:21