Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations gkittelson on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Add Comma to a number String

Status
Not open for further replies.

cisokay

Programmer
Apr 13, 2005
2
US
I have a string of number values such as

60000 and I would like it to be reprensed 60,000 and I was wondering if someone can help me out with a few lines to do the conversion. Thanks.
 
In place or in a new string?..
It's not few lines (more than one-two-three;)...
 
It will be a new string, I know editing current string is near impossible or just plain hard. From messing around with some code you are right, its more than a few lines. =[
 
Warning: no arg checking (null ptr, target overflow):
Code:
#include <string.h>
#include <stdio.h>

static const char* digits = "0123456789";
static const char  comma  = ',';

static int insCommas(char* t, const char* s, int n)
{
  int	i, m, j = 0, k = 0;
  int	nres = n + (n-1)/3;

  m = n % 3;
  while (n > 0)
  {
    for (i = 0; i < m; ++i)
    {
	t[j++] = s[k++];
    }
    n -= m;
    if (m && n > 0)
	t[j++] = comma;
    m = 3;
  }
  return nres;
}

int InsCommas(char* t, const char* s)
{
  int	i, k, m, n = 0;
  const char* p;

  while ((p = strpbrk(s,digits)) != 0)
  {
    k = p - s;
    for (i = 0; i < k; ++i)
	t[i] = s[i];
    t += k;
    n += k;
		
    k = strspn(p,digits);
    m = insCommas(t,p,k);
    t += m;
    s = p + k;
    n += m;
  }
  *t = 0;
  
  return n;
}
/* The sample */
int main()
{
	char t[80];
	memset(t,0,sizeof t);
	int k;

	k = IntCommas(t,"123  1234567890  5678");
	printf("%.40s<-%d\n",t,k);

	return 0;
}
(and No comments;)...
 
Can someone tell me why this doesn't work
Code:
#include <stdio.h>
 
main ()
 
{
 
char *instring = "12345678";
char outstring[64];
 
sprintf ( outstring, "%'d", atoi ( instring ) );
 
printf ( "%s\n", outstring );
 
}
when I man printf it shows ' as the option to insert the thousands separator but when I try this ( AIX C and gcc ) it doesn't seem to put them in.

Columb Healy
 
This flag is a GNU extension. It depends on current locale settings for LC_NUMERIC category (no automatic thousands comma separators).
 
Sorry, this is just a method description, not actual code, but it's not hard.

Method A (not in place):

Initialize digit-counter to zero.
start at end of string.
repeat until start of string (working backwards):
{
Copy digits to a new string one by one, incrementing digit-counter each time.
If "digit" is a space, reset digit counter to zero.
If digit-counter is three, put a comma in new string and reset digit counter.
}
Now reverse the whole string (very easy).

Method B (in place, but remember the string needs to have extra space available for the commas)

start at end of string with digit counter as above, but keep a spare length-of-new-string counter, initialized as zero.
Behave exactly as method A except:
(1) increment length-of-new-string each time you go round the loop
(2) on inserting a comma, first move the whole string from existing point forwards one space. You'll need the length-of-new-string so you know how much to move.
 
lionelhill
Something like this
Code:
#include <stdio.h>
 
main ()
 
{
 
char *instring = "1234890";
char outstring[64];
char *ptr, *optr;
int i, length, commas;
 
/* move ptr to end of instring */
for ( ptr = instring; *ptr; ptr++ ); 
 
/*calculate offset with commas*/
length = ptr - instring;
commas = ( length - 1 ) / 3;
optr = outstring + length + commas;   
 
/*copy instring into outstring backwards inserting commas */
*optr-- = *ptr--;
for ( i = 1; ptr >= instring; i++ )
  {
  *optr-- = *ptr--;
  if ( ( i % 3 ) == 0 )
    *optr-- = ',';
  }
 
/* show the result */
printf ( "%s\n", outstring );
}

Columb Healy
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top