Follow along with the video below to see how to install our site as a web app on your home screen.
Note: This feature may not be available in some browsers.
#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;
}
#include <stdio.h>
main ()
{
char *instring = "12345678";
char outstring[64];
sprintf ( outstring, "%'d", atoi ( instring ) );
printf ( "%s\n", outstring );
}
#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 );
}