For example: Enter the string "1234" and return the number 1234. input string "+1234", returns the number 1234. input string "-1234", Returns the number -1234. #include <stdio.h>int my_atoi (char s[10]) { int flag=1; //the sign of the symbol bit int ret=0; //is used to store the number taken out char *p=s; if (*p== '-') { flag=-1; //if a negative number is entered, the symbol position is the minus sign first } if (*p== ' + ' | | *p== '-') { p++; //if the first bit of the input is a sign bit, skip it, and the pointer points to the next } while (*p!= ') { if ((*p>= ' 0 ') && (*p<= ' 9 ')) { ret=ret*10+*p-' 0 '; //*p-' 0 ' Refers to the number that is taken out each time p++; } else //if the input is not a number, the pointer points to the next p++; } return flag*ret; //the number and sign bits taken out (i.e. flag=1 or flag= -1) Multiply}int main () { int n; //the corresponding integer to be output at last char str10]; gets (str); //receives a string n=my_atoi (str); printf ("%d\n", N); return 0;}
This article from "Thanksgiving" blog, reproduced please contact the author!
Implement a function int my_atoi (char s[]), you can convert a string to the corresponding integer