I just think the output should be 0 because there is an invalid world "a" in the string. How can I understand the logic of solving this problem?
Because you should read the string until you get smth like a 'a' ( not digit ).
So you read '-' , skip two zeros, read '1' and '2' then get 'a' and stop.
Requirements for atoi:
The string can contain additional characters after those that form the
integral number, which are ignored and have no effect on the behavior
of this function.