If you want to go to the dark side, try this:
Code:
#include <iostream>
#include <cstring>
using std::cout;
using std::endl;
using std::cin;
// Commasplice large numbers
static char *commaSplice (char *sz);
int main ()
{
// User input commasplice variable
char c[255];
// Used by for loop
int i;
// If the user enters a bad number to be commaspliced
ENTER_NUMBER_AGAIN:
// Ask for number
cout << "Enter a number to be spliced please:" << endl;
cin.getline (c, 255);
// Make sure the string is all numerical
for (i = 0; i < strlen (c) && isdigit (c[i]); i++);
if (!isdigit (c[--i]))
goto ENTER_NUMBER_AGAIN;
// Convert the string number into commaspiced art
strncpy (c, commaSplice (c), sizeof (c));
// Print out the spliced version of the number input
cout << "Comma Spliced:\n" << c << endl;
return 0;
}
// Commasplice large numbers
static char *commaSplice (
char *sz) // The string that needs to get spliced
{
// How long the string is; also used as a current pointer to our original string
int l = strlen (sz);
// Incrementer on how many numbers we've covered
int i = 0;
// How many commas are going to be needed?
int c = (l - 1) / 3;
// Our commaSpliced version of the incoming string.
char *szS = new char[strlen (sz) + 1 + c];
// Current location of current string PLUS commas
int lS = strlen (sz) + c;
// Work backwards of the string we need to commasplice and
// add ',' where appropriate.
NEXT_CHARACTER:
// Make sure our current location is inside the original string
if (--l < 0)
return szS;
// Increment the total numbers we've moved
i++;
// Add the latest char from original to commaSpliced string
szS[--lS] = sz[l];
// Add comma when needed
if (i % 3 == 0)
szS[--lS] = ',';
// Proceed to the next character to move
goto NEXT_CHARACTER;
}