Thread: Adding a comma to numbe string

  1. #1
    Registered User
    Join Date
    Apr 2005
    Posts
    53

    Adding a comma to numbe string

    Given a string that has a number such as:

    6000 or 10000

    I want to add ',' to the value so it reads

    6,000 or 10,000

    would be good up to about million or so. Thank you.

  2. #2
    Registered User
    Join Date
    Mar 2004
    Posts
    494
    Ok so what have you done so far? Show us some code or where your stuck and we will try to point you in the right direction, but no one will do the work for you and just hand it over to you.
    When no one helps you out. Call google();

  3. #3
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,897
    This has been asked and answered before. A forum search for "commaize" would find you at least one example in C and one and C++.
    My best code is written with the delete key.

  4. #4
    Registered User
    Join Date
    Apr 2005
    Posts
    53
    I'm new to C and This what I have so far in my function:

    Code:
        char string[20];
        char new[20];
        sprintf(string,"%d",total);
        int len = strlen(string);
        int cnt = 0;
    
        for(int i = len; string[i] != NULL; i--)
        {
          strcpy(new[i],string[];
          cnt++;
          if(cnt == 3) {
            i += 1;
            stncpy(new[i],",");
          }
         }

  5. #5
    Registered User
    Join Date
    Apr 2005
    Posts
    53
    I searched the forums, I only found one for C++ but no references to C.

  6. #6
    ---
    Join Date
    May 2004
    Posts
    1,379
    off the top of my head, what you would need to do is copy characters to a new string, backwards, one at a time. Every three characters, insert a ',' and then continue copying from the original string. The i suppose you need to reverse the characters around again...

  7. #7
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,897
    >I searched the forums, I only found one for C++ but no references to C.
    I gave you a search string to use, that means I know that the code is there (since I wrote it). You'll need to read a bit though, it's not right at the top with big flashing letters that say "FOR CISOKAY".
    My best code is written with the delete key.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. We Got _DEBUG Errors
    By Tonto in forum Windows Programming
    Replies: 5
    Last Post: 12-22-2006, 05:45 PM
  2. Replies: 4
    Last Post: 03-03-2006, 02:11 AM
  3. Adding elements to a string
    By Vicked in forum C Programming
    Replies: 2
    Last Post: 04-10-2003, 12:52 PM
  4. creating class, and linking files
    By JCK in forum C++ Programming
    Replies: 12
    Last Post: 12-08-2002, 02:45 PM
  5. Warnings, warnings, warnings?
    By spentdome in forum C Programming
    Replies: 25
    Last Post: 05-27-2002, 06:49 PM