In article <1103068973.18047@sj-nntpcache-3> pb <> wrote:
>Im wanted to pad out blank spaces with a specific character instead of
>spaces or zeros, does C support that?
No.
>printf("$%*d", '*', 5); // Not sure what the format string is supposed to
>look like to do this
(Note that //-comments wrap around, so that if this had been intended
to be a code example, it would not have worked so well.
The "*" in "%*d" is a field-width specifier that reads an "int"
argument from the argument list, so:
printf("%*d", 2, 5);
prints the value "5" in a ten-character field. The field is blank
or zero padded depending on the pad option selected: blank by
default, zero if you use a 0 modifier.)
>example output i would want is this:
>$********5
There is no standard way to do this. It is easy to build your own
though: just sprintf() the numeric value, and then work with the
string. In this case, to get an integer printed into a ten digit
field and replace leading blanks or zeros with spaces, just do
something like:
char buf[SOME_SIZE]; /* must be at least 11 chars */
int val;
...
sprintf(buf, "%010d", val); /* produces, e.g., 0000000005 */
subst(buf, '0', '*');
where the subst() function reads:
/*
* Do substitutions on leading characters in the given string:
* Replace all occurrences of "from" with "to". (We assume
* from != '\0'.)
*/
void subst(char *s, char from, char to) {
while (*s == from)
*s++ = to;
}
Note that if you print with leading blanks, you will need to subst()
from ' ' instead of '0'. (This trick works either way.)
--
In-Real-Life: Chris Torek, Wind River Systems
Salt Lake City, UT, USA (40°39.22'N, 111°50.29'W) +1 801 277 2603
email: forget about it
http://web.torek.net/torek/index.html
Reading email is like searching for food in the garbage, thanks to spammers.