Path: utzoo!utgpu!cs.utexas.edu!wuarchive!psuvax1!rutgers!mcnc!duke!drh From: drh@duke.cs.duke.edu (D. Richard Hipp) Newsgroups: alt.sources Subject: xprintf update Message-ID: <659977812@romeo.cs.duke.edu> Date: 30 Nov 90 15:10:14 GMT Organization: Duke University Computer Science Dept.; Durham, N.C. Lines: 66 brad@hcx1.ssd.csd.harris.com (Brad Appleton) writes (via mail): >.... I happened to be thinking that >in addition to stuff like: > > printf( "%10s", "str" ) >and > printf( "%-10s", "str" ) > >to do right & left justification, that it would be *_really_* nice >to also have something like: > > printf( "%=10s", "str" ) > >that would print "str" centered within a ten character field. Good idea! It turns out that a "=" flag for centering is easy to implement in the xprintf sources I posted yesterday. The diffs are shown below, together with a test program and the test program's output. The output of "diff xprintf.c newversion.c" is: 293a294 > int flag_center; /* True if "=" flag is present */ 340a342 > case '=': flag_center = 1; c = 0; break; 343a346 > if( flag_center ) flag_leftjustify = 0; 624a628,632 > if( flag_center ){ > nspace = nspace/2; > width -= nspace; > flag_leftjustify = 1; > } A program to test Brad's idea: void main(void){ int i; for(i=1; i<27; i++) printf("[%=20.*s]\n",i,"abcdefghijklmnopqrstuvwxyz"); } The output of the test program: [ a ] [ ab ] [ abc ] [ abcd ] [ abcde ] [ abcdef ] [ abcdefg ] [ abcdefgh ] [ abcdefghi ] [ abcdefghij ] [ abcdefghijk ] [ abcdefghijkl ] [ abcdefghijklm ] [ abcdefghijklmn ] [ abcdefghijklmno ] [ abcdefghijklmnop ] [ abcdefghijklmnopq ] [ abcdefghijklmnopqr ] [abcdefghijklmnopqrs ] [abcdefghijklmnopqrst] [abcdefghijklmnopqrstu] [abcdefghijklmnopqrstuv] [abcdefghijklmnopqrstuvw] [abcdefghijklmnopqrstuvwx] [abcdefghijklmnopqrstuvwxy] [abcdefghijklmnopqrstuvwxyz] Brought to you by Super Global Mega Corp .com