mercredi 22 avril 2015

C++ : What is the usage of int('0') in this code?

This code Find the sum of all digits that occur in a string.

Example

sumUpNumbers("2 apples, 12 oranges") = 5 //2+1+2

Can anyone explain the need for use int('0') in this code!?

int sumUpDigits(std::string inputString) {

  int answer = 0;

  for (int i = 0; i < inputString.size(); i++) {  
    if ('1' <= inputString[i] && inputString[i] <= '9') {
      answer += int(inputString[i]) - int('0');
    }
  }

  return answer;
}

Aucun commentaire:

Enregistrer un commentaire