Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C Programming > printf padding with alternate character?

Reply
Thread Tools

printf padding with alternate character?

 
 
pb
Guest
Posts: n/a
 
      12-14-2004
Im wanted to pad out blank spaces with a specific character instead of
spaces or zeros, does C support that?

printf("$%*d", '*', 5); // Not sure what the format string is supposed to
look like to do this

example output i would want is this:
$********5


 
Reply With Quote
 
 
 
 
Ben Pfaff
Guest
Posts: n/a
 
      12-15-2004
"pb" <(E-Mail Removed)> writes:

> Im wanted to pad out blank spaces with a specific character instead of
> spaces or zeros, does C support that?


No. You will have to write your own code to do it.
--
Ben Pfaff
email: http://www.velocityreviews.com/forums/(E-Mail Removed)
web: http://benpfaff.org
 
Reply With Quote
 
 
 
 
Chris Torek
Guest
Posts: n/a
 
      12-15-2004
In article <1103068973.18047@sj-nntpcache-3> pb <(E-Mail Removed)> 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 (4039.22'N, 11150.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.
 
Reply With Quote
 
Peter Nilsson
Guest
Posts: n/a
 
      12-15-2004
pb wrote:
> Im wanted to pad out blank spaces with a specific character instead

of
> spaces or zeros, does C support that?


Yes, but not directly through a standard function.

> printf("$%*d", '*', 5); // Not sure what the format string is

supposed to
> look like to do this
>
> example output i would want is this:
> $********5


#include <stdio.h>
#include <string.h>

#define HASH "*********"

int main(void)
{
int amount = 520;
char number[100];
sprintf(number, "%d.%02d", amount / 100, amount % 100);
printf( "$%.*s%s\n",
(int) (sizeof HASH - 1 - strlen(number)),
HASH,
number );
return 0;
}

BTW, $ is not a member of the basic character set.

--
Peter

 
Reply With Quote
 
Mike Wahler
Guest
Posts: n/a
 
      12-15-2004

"pb" <(E-Mail Removed)> wrote in message
news:1103068973.18047@sj-nntpcache-3...
> Im wanted to pad out blank spaces with a specific character instead of
> spaces or zeros, does C support that?
>
> printf("$%*d", '*', 5); // Not sure what the format string is supposed to
> look like to do this
>
> example output i would want is this:
> $********5


You've got many good answers already. Here's another
alternative:

#include <iostream>
#include <string>
#include <stdio.h>

unsigned int digits(int value, unsigned int radix)
{
unsigned int result = 0;

if(value < 0)
value *= -1;

result = !value;

while(value)
{
++result;
value /= radix;
}

return result;
}

int main()
{
int value = 42;
int wid = 5;
char prefix = '$';
int leading = 0;
char pad = '*';
int i = 0;
unsigned int d = digits(value, 10) + (value < 0);

if(d > wid)
wid = d;

leading = wid - d ;

putchar(prefix);

for(i = 0; i < leading; ++i)
putchar(pad);

printf("%d\n", value);
return 0;
}

-Mike


 
Reply With Quote
 
Martin Ambuhl
Guest
Posts: n/a
 
      12-15-2004
Mike Wahler wrote:

> You've got many good answers already. Here's another
> alternative:
>
> #include <iostream>
> #include <string>
> #include <stdio.h>


Did you forget this was comp.lang.c? It's a good thing, since you might
have gotten flamed in comp.lang.c++ for <stdio.h> instead of <cstdio>,
or, in their anti-C exuberance, for using either.
 
Reply With Quote
 
Mike Wahler
Guest
Posts: n/a
 
      12-15-2004

"Martin Ambuhl" <(E-Mail Removed)> wrote in message
news:(E-Mail Removed)...
> Mike Wahler wrote:
>
> > You've got many good answers already. Here's another
> > alternative:
> >
> > #include <iostream>
> > #include <string>
> > #include <stdio.h>

>
> Did you forget this was comp.lang.c?


Actually, no. Those C++ headers are 'residue' from
a 'scratch' file I forgot to delete. (I suppose they
must have been scrolled off the screen.)

> It's a good thing, since you might
> have gotten flamed in comp.lang.c++ for <stdio.h> instead of <cstdio>,


Any flames about that would be unjustified. <stdio.h>
is as valid a standard header in C++ as in C. But yes,
I know, some folks don't know any better.

> or, in their anti-C exuberance, for using either.


Let's not go there.

But thanks for pointing out my error.
I'll try to pay better attention in the future.

-Mike


 
Reply With Quote
 
Lawrence Kirby
Guest
Posts: n/a
 
      12-15-2004
On Wed, 15 Dec 2004 00:37:38 +0000, Chris Torek wrote:

....

> printf("%*d", 2, 5);
>
> prints the value "5" in a ten-character field.


Looks like a 2 character field to me.

> 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.)


That depends on whether you want 0 to be output as ********** or
*********0

Lawrence
 
Reply With Quote
 
Zoran Cutura
Guest
Posts: n/a
 
      12-15-2004
Chris Torek <(E-Mail Removed)> wrote:
> In article <1103068973.18047@sj-nntpcache-3> pb <(E-Mail Removed)> wrote:

....
> printf("%*d", 2, 5);

^^^
10???
>
> prints the value "5" in a ten-character field. The field is blank

^^^
two???
--
Z ((E-Mail Removed))
"LISP is worth learning for the profound enlightenment experience
you will have when you finally get it; that experience will make you
a better programmer for the rest of your days." -- Eric S. Raymond
 
Reply With Quote
 
Chris Torek
Guest
Posts: n/a
 
      12-15-2004
>On Wed, 15 Dec 2004 00:37:38 +0000, Chris Torek wrote:
>> printf("%*d", 2, 5);
>> prints the value "5" in a ten-character field.


In article <(E-Mail Removed)>
Lawrence Kirby <(E-Mail Removed)> wrote:
>Looks like a 2 character field to me.


Oops. Hasty posting....

>> Note that if you print with leading blanks, you will need to subst()
>> from ' ' instead of '0'. (This trick works either way.)

>
>That depends on whether you want 0 to be output as ********** or
>*********0


Right, something else I managed to forget to bring up. And if this
is intended for printing money-amounts, one may have to fiddle with
negative numbers as more special cases.
--
In-Real-Life: Chris Torek, Wind River Systems
Salt Lake City, UT, USA (4039.22'N, 11150.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.
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
what's 'alternate' in <link rel=alternate>? Chris ASP .Net 2 03-04-2007 04:45 PM
Alternate initializers or alternate class? transfire@gmail.com Ruby 10 07-17-2006 03:20 AM
printf() formatting - stripping zeroes, padding Piotr B. C++ 2 10-19-2004 07:29 PM
printf() formatting - stripping zeroes, padding Michal Prinke C Programming 0 10-19-2004 03:28 PM
printf() formatting - stripping zeroes, padding Michal Prinke C++ 0 10-19-2004 03:28 PM



Advertisments