#include #define isnum(x) ('0' <= (x) && (x) <= '9') #define tonum(x) ((x) - '0') char str[] = "M 123 12 45 478 235 124 12 9"; int x[8]; // Returns: // Number of variables matched // 0 - no variables were matched int myscanf(char *str, char *format) { int n = 0; // number of matches int *xp; xp = x; for(; format != '\0'; format++) { switch(*format) { case '%': format++; switch(*format) { case 'd': *xp = 0; if(!isnum(*str)) return n; for(; isnum(*str); str++) { *xp *= 10; *xp += tonum(*str); } xp++; n++; } break; default: // want *str == *format if(*str == '\0' || *str == '\n' || *str == '\r') return n; if(*str != *format) return n; str++; break; } } return n; } // Returns number of variables matched int mysearch(char *str, char *format) { int n; for(; *str != '\0'; str++) { n = myscanf(str, format); if(n) return n; } return 0; } int main() { int i; printf("%d\n", myscanf(str, "M %d %d %d %d %d %d %d %d %d")); printf("%d\n", mysearch(str, "M %d %d %d %d %d %d %d %d %d")); printf("%d %d %d %d\n", x[0], x[1], x[2], x[3]); return 0; }