function - C++ : What is the usage of int('0') in this code? -
this question has answer here:
- how convert single char int 9 answers
this code find sum of digits occur in string.
example
sumupnumbers("2 apples, 12 oranges") = 5 //2+1+2
can explain need use int('0') in code!?
int sumupdigits(std::string inputstring) { int answer = 0; (int = 0; < inputstring.size(); i++) { if ('1' <= inputstring[i] && inputstring[i] <= '9') { answer += int(inputstring[i]) - int('0'); } } return answer; }
it converts char ascii code make number out of string
int('9') - int('0') = 9