What is the purpose of using str[i]-'0' where str is a string?
<p>I was going for the solution of a problem i had solved by myself. Many solutions i came across used this notation of str[i]-'0' to perform calculation on string str which has digits stored in it. </p>

<p>The code below compares two such strings, to count the digits which have same index position in both the strings and also maintains a separate count for the digits which appear in both the strings but don't have the same index.</p>

<p>My question is what is the purpose of guess[i] - '0'. How does it work in particular because i have been using int a = guess[i] all the time and want to know how the other method is better.</p>

<pre><code>class Solution {
public:
    string getHint(string secret, string guess) {
        vector&lt;int&gt;tb_guess(10),tb_secret(10);
        int A=0,B=0;
        for (int i=0;i&lt;secret.size();++i){
            if (secret[i]==guess[i]) A++;
            else {
                tb_guess[guess[i]-'0']++;
                tb_secret[secret[i]-'0']++;
            }
        }
        for (int i=0;i&lt;10;++i){
            B=B+ min(tb_guess[i],tb_secret[i]);
        }
        return to_string(A)+'A'+to_string(B)+'B';
    }
};
</code></pre>