/* Input an integer and output it with commas every three digits. */

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

#define	N	256		/* maximum string length, including the '\0' */

main()
{
	char source[N];		/* hold the digits without commas */
	char dest[N];		/* hold the digits with commas */

	int s = 0;		/* index of each char in source */
	int d = 0;		/* index of each char in dest */
	int count;		/* count groups of three digits */

	printf("Please input an integer and press RETURN.\n");
	scanf("%s", source);

	/* Copy the chars from source into dest, including the trailing
	'\0'.  Insert a comma every three digits. */

	for (count = strlen(source); count >= 0; --count) {
		dest[d] = source[s];		/* Copy a character. */
		s++;
		d++;
		if (count % 3 == 1 && count > 1 && dest[d-1] != '-') {
			dest[d] = ',';
			d++;
		}
	}

	printf("%s\n", dest);
}