#include <stdlib.h> int intval(const char* str){ if(str==NULL){ return -1; } char* p = str; int result=0; int flag = 1; char c; while(c = *p++){ if(c=='-'){ flag = -1; } if(c>'0'&&c<'9'){ result = result*10+c-'0'; } } return flag*result; } int main(){ printf("%s intval结果:%d\r\n","a123b",intval("a-123b")); //a123b intval结果:-123 return 1; }