[C Language] implements a function int my_atoi (char s []) to convert a string to a corresponding integer.
# Include <stdio. h> int my_atoi (char s [5]) {int flag = 1; // in this case, int ret = 0; char * p = s; if (* p = '-') // if the first character is '-', the flag switch is enabled and set to-1; {flag =-1 ;} if (* p = '+' | * p = '-') // if the first character is '-' or '+', no processing is performed, jump directly to the second character {p ++;} while (* p! = '\ 0') {if (* p> = '0') & (* p <= '9') // because it must be an integer, so only 0 ~ Number between 9 {ret = ret * 10 + * p-'0'; // obtain each digit, and then accumulate p ++ according to the law in decimal ;}} return flag * ret;} int main () {int ret = 0; char s [5]; gets (s); ret = my_atoi (s ); // receives the return value of the atoi function printf ("% d \ n", ret); return 0 ;}