编程实现atoi函数

#include   
#include   
  
#define MAX_SIZE 100  
  
int my_atoi(char *src)  
{  
    int flag = 1;  
      
    int result = 0;  
      
    if(*src == '-')  
    {  
        flag = -1;  
    src++;  
    }  
 
    while(*src != '\0')  
    {  
        if(*src >= '0' && *src <= '9')  
    {  
        result = result * 10 + (*src - '0');  
    }  
    else  
    {  
        break;  
    }  
      
    src++;  
    }  
 
    return result * flag;  
}  
  
int main()  
{  
    int i;  
    int len;  
    int result = 0;  
     
    char src[MAX_SIZE] = {0};  
  
    printf("Please input string:\n");  
    gets(src);  
      
    result = my_atoi(src);  
  
    printf("result = %d\n",result);  
  
    return 0;  
}  

你可能感兴趣的:(C语言)