Stringtoint function

why are we subtracting 0 in stringtoint function
i.e. in line
ans+= ((str[i]-‘0’)*p);

Hello @aviralpandey003,

String is a collection of characters like: “abc”, “123” and “a_9h+”
And each character is identified by ASCII numbers.
You may refer to the following chart:
image

In this question we are talking specifically about the string of digits.

The ASCII number for ‘0’ (a character not integer) is 48
Suppose str[i] contains ‘0’
So, atr[i]-‘0’ = ‘0’-‘0’ = 48-48 =0(this is an integer)

Similarly for other character digits.

Hope, this would help.

I hope I’ve cleared your doubt. I ask you to please rate your experience here
Your feedback is very important. It helps us improve our platform and hence provide you
the learning experience you deserve.

On the off chance, you still have some questions or not find the answers satisfactory, you may reopen
the doubt.