1 for(p = fmt; *p; p++)2 {3 if(*p! ='%')4 {5Putchar (*p);6 Continue;7 }8i =0;//always point to the next character, since 0 o'clock also has no characters, and may end up with no characters9localfmt[i++]='%';//start local FMTTen while(* (p+1) &&!isalpha (* (p+1)) )//p is only processed to the current character test next character ++p test p+1 One //I process to the next character starting from 0 add using i++ Alocalfmt[i++]=* (++P);//Collect chars -localfmt[i++]=* (p+1);//format Letter -localfmt[i]=' /'; the - Switch(*++p)//format Letter - { - + } -}
#include <stdio.h>#include<ctype.h>#include<stdarg.h>voidminprintf (Char*FMT, ...) {va_list ap; Char*p, *Sval; intIval,i; DoubleDval; Charlocalfmt[ -]; Va_start (AP, FMT); for(p = fmt; *p; p++) { if(*p! ='%') {Putchar (*p); Continue; } I=0; Localfmt[i++]='%'; while(* (p+1) &&!isalpha (* (p+1)) Localfmt[i++]=*(++Q); Localfmt[i++]=* (p+1); Localfmt[i]=' /'; Switch(*++p) { Case 'D': Ival= Va_arg (AP,int); printf (localfmt,ival); Break; Case 'F': Dval= Va_arg (AP,Double); printf (localfmt,dval); Break; Case 's': for(Sval = Va_arg (AP,Char*); *sval; sval++) Putchar (*sval); Break; default: Putchar (*p); Break; }} va_end (AP);}intMain () {minprintf ("% d",Ten); return 0;}Full Code
For the start of the use of the number of i++ always point to the location to write (the next position of the character), because there may be nothing to write, and finally processed to the empty position I.
For existing tests, use the next character (P+1), and then first move the pointer over the current character, ++p.
Less code, confusing boundaries and starting