Thread: questions about commas again

  1. #1
    CuriousNoob
    Guest

    commas

    i saw this post and i was wondering if any1 knows how to do that...

    hug
    Junior Member

    Registered: Dec 2002
    Location:
    Posts: 1

    comma
    Given a variable between 0 and 2,000,000 how to print out with proper commas
    Eg;if we input 12345 the output should be 12,345

  2. #2
    Registered User dizolve's Avatar
    Join Date
    Dec 2002
    Posts
    68
    Here's my function for it:

    Code:
    char *comma(char *str) {
      static char buffer[MAX_BUFFER];
      int len = strlen(str), i, p = len + (strlen(str) / 3);
    
      if (!(len %3)) { p--; }
    
      for (i = -1; len >= 0; len--) {
        if (i == 3) {
          buffer[p--] = ',';
          i = 0;
        }
    
        buffer[p--] = str[len];
        i++;
      }
      
      return buffer;
    }
    The code could be cleaner, but it's from a long time ago. Note that you have to pass it a string ("12345"). You could probably change it easily to accept an int.

      __               &n bsp;      ___ & nbsp;       &nb sp;       &nbsp ;    
     /\ \  __    &nbs p;           /\_ \      &nbsp ;        & nbsp;     
     \_\ \/\_\  ____     _ __\//\ \    __  __&n bsp;    __   
     /'_` \/\ \/\_ ,`\  / __`\\ \ \  /\ \/\ \  /'__`\ 
    /\ \_\ \ \ \/_/  /_/\ \_\ \\_\ \_\ \ \_/ |/\  __/ 
    \ \___,_\ \_\/\____\ \____//\____\\ \___/ \ \____\
     \/__,_ /\/_/\/____/\/___/ \/____/ \/__/   \/____/
            &n bsp; I have a BAD figlet& nbsp;addiction.

  3. #3
    CuriousNoob
    Guest

    Cool thanks

    thats great...but what are there those asterisk things there for...

  4. #4
    Registered User dizolve's Avatar
    Join Date
    Dec 2002
    Posts
    68

      __               &n bsp;      ___ & nbsp;       &nb sp;       &nbsp ;    
     /\ \  __    &nbs p;           /\_ \      &nbsp ;        & nbsp;     
     \_\ \/\_\  ____     _ __\//\ \    __  __&n bsp;    __   
     /'_` \/\ \/\_ ,`\  / __`\\ \ \  /\ \/\ \  /'__`\ 
    /\ \_\ \ \ \/_/  /_/\ \_\ \\_\ \_\ \ \_/ |/\  __/ 
    \ \___,_\ \_\/\____\ \____//\____\\ \___/ \ \____\
     \/__,_ /\/_/\/____/\/___/ \/____/ \/__/   \/____/
            &n bsp; I have a BAD figlet& nbsp;addiction.

  5. #5
    CuriousNoob
    Guest

    hm...

    is this all c or is this c++?

  6. #6
    Registered User dizolve's Avatar
    Join Date
    Dec 2002
    Posts
    68
    Everything we've discussed so far is C.

      __               &n bsp;      ___ & nbsp;       &nb sp;       &nbsp ;    
     /\ \  __    &nbs p;           /\_ \      &nbsp ;        & nbsp;     
     \_\ \/\_\  ____     _ __\//\ \    __  __&n bsp;    __   
     /'_` \/\ \/\_ ,`\  / __`\\ \ \  /\ \/\ \  /'__`\ 
    /\ \_\ \ \ \/_/  /_/\ \_\ \\_\ \_\ \ \_/ |/\  __/ 
    \ \___,_\ \_\/\____\ \____//\____\\ \___/ \ \____\
     \/__,_ /\/_/\/____/\/___/ \/____/ \/__/   \/____/
            &n bsp; I have a BAD figlet& nbsp;addiction.

  7. #7
    CuriousNoob
    Guest

    hm...again

    is there any way to rewrite this function without using the pointer stuff?

  8. #8
    Registered User dizolve's Avatar
    Join Date
    Dec 2002
    Posts
    68
    well, not really.. not the way I use it anyhow. The only way you can really avoid pointers is to put this inline (actually use the function code over and over in places you need it) which is _not_ recommended because if you need to change something you have to change all the instances of it. I'd say to just learn about pointers, they're not too complicated.

      __               &n bsp;      ___ & nbsp;       &nb sp;       &nbsp ;    
     /\ \  __    &nbs p;           /\_ \      &nbsp ;        & nbsp;     
     \_\ \/\_\  ____     _ __\//\ \    __  __&n bsp;    __   
     /'_` \/\ \/\_ ,`\  / __`\\ \ \  /\ \/\ \  /'__`\ 
    /\ \_\ \ \ \/_/  /_/\ \_\ \\_\ \_\ \ \_/ |/\  __/ 
    \ \___,_\ \_\/\____\ \____//\____\\ \___/ \ \____\
     \/__,_ /\/_/\/____/\/___/ \/____/ \/__/   \/____/
            &n bsp; I have a BAD figlet& nbsp;addiction.

  9. #9
    CuriousNoob
    Guest

    questions about commas again

    i was wondering if someone could write this function without using <string.h> or pointers?

    Code:
    char *comma(char *str)
    {
         static char buffer[MAX_BUFFER];
         int len = strlen(str), i, p = len + (strlen(str) / 3);
    
         if (!(len %3)) 
         { 
               p--;
         }
    
         for (i = -1; len >= 0; len--)
         {
            if (i == 3)
            {
                 buffer[p--] = ',';
                 i = 0;
            }
           
            buffer[p--] = str[len];
            i++;
        }
      
         return buffer;
    }

  10. #10
    End Of Line Hammer's Avatar
    Join Date
    Apr 2002
    Posts
    6,231
    >>i was wondering if someone could write this function without using <string.h> or pointers?
    Do it yourself. Or at least make an effort to do so. When you're stuck, someone will help.

    I merged your threads, btw. Don't start a new one on the same subject please.
    When all else fails, read the instructions.
    If you're posting code, use code tags: [code] /* insert code here */ [/code]

  11. #11
    CuriousNoob
    Guest

    ok

    well, what i dont understand is what this line of code means :

    Code:
    int len = strlen(str), i, p = len + (strlen(str) / 3);
    i know that 'i' is the counter. but as far as that len = strlen(str) and p = len +( strlen(str) / 3 ) goes, im not to sure. Does 'strlen' mean the 'string lenght' ?

  12. #12
    End Of Line Hammer's Avatar
    Join Date
    Apr 2002
    Posts
    6,231
    >>int len = strlen(str), i, p = len + (strlen(str) / 3);
    This declares some variables and populates some of them with initial values. All are of type int.

    strlen() is a standard function found in string.h, that determines the length of the string.

    This bit:
    >>p = len + (strlen(str) / 3);
    is performing a calculation and assigning the result to p. It's actually a poor example as calculating the length of the string via strlen() is pointless, we've already done that to set up the len variable. It would have been more efficient to write:
    >>p = len + (len / 3);

    Note that i is not assigned an initial value, so the first thing we must do with i in the function is give it a value:
    >>for (i = -1; ...
    When all else fails, read the instructions.
    If you're posting code, use code tags: [code] /* insert code here */ [/code]

  13. #13
    Curiousnoob
    Guest

    Thumbs up i got it!

    ok! i figured out how to change it so that i dont need string.h or the pointers. but i still dont understand what each line is doing. could someone fill me in on the lines where i dont have comments?

    Code:
    void print_commas( char number[] )
    {
    	char format[ MAX ] ;			/* The number formatted with commas. */ 
    	int i, length, p ;				/* ?? what is the p for? */
    
    	length = string_length( number ) ;	/* Get the length of the string */
    
    	p = length + length / 3 ;		/* ?? I dont get this line */
    
    	if ( ! ( length % 3 ) )			/* ?? This one either */
    		p--;
    	
    	for ( i = -1 ; length >= 0 ; length-- )	/* Srat at the last element of the array and work down */
    	{
    		if ( i == 3 )			/* Every 3 elements, print a comma */
    		{
    			format[ p-- ] = ',';
    			i = 0 ;			/* Then rest the counter */
    		}
    	
    		format[ p-- ] = number[ length ] ;	/* ?? Whats happening here? */
    		i++ ;						/* Increment i */
    	}
      
    	printf( "Formatted      : %s\n", format ) ;	/* Display the formatted number */
    }

  14. #14
    Registered User dizolve's Avatar
    Join Date
    Dec 2002
    Posts
    68
    p keeps track of the place in the buffer, it's needed since commas are being added in.

    Since commas go every 3 places, we have to parse the number in reverse order, this gives the number of characters that the number with commas will take up.
    Code:
    p = length + length / 3;
    This line checks to see if the remainder of length / 3 is 0, since for example, 999999 is 6 digits, 6 / 3 = 2 therefor p = length + 2, which is wrong since there's only one comma needed. So, if length % 3 is 0, we decrease p to decrease the buffer length by one comma and prevent errors.
    Code:
    if ( ! ( length % 3 ) )			/* ?? This one either */
    		p--;
    Again, we're parsing in reverse, so now we take the next digit from number and put it in the buffer.
    Code:
    format[ p-- ] = number[ length ] ;	/* ?? Whats happening here? */
    Once again, I coded this a long time ago and it's been hacked at, it's not as efficient or clean as it could be but it works.
    Last edited by dizolve; 01-11-2003 at 09:09 PM.

    &nbsp;&nbsp;__&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&n bsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;___&nbsp;& nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nb sp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp ;&nbsp;&nbsp;&nbsp;&nbsp;
    &nbsp;/\&nbsp;\&nbsp;&nbsp;__&nbsp;&nbsp;&nbsp;&nbsp;&nbs p;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;/\_&nbsp;\&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp ;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;& nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
    &nbsp;\_\&nbsp;\/\_\&nbsp;&nbsp;____&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;_ __\//\&nbsp;\&nbsp;&nbsp;&nbsp;&nbsp;__&nbsp;&nbsp;__&n bsp;&nbsp;&nbsp;&nbsp;&nbsp;__&nbsp;&nbsp;&nbsp;
    &nbsp;/'_`&nbsp;\/\&nbsp;\/\_&nbsp;,`\&nbsp;&nbsp;/&nbsp;__`\\&nbsp;\&nbsp;\&nbsp;&nbsp;/\&nbsp;\/\&nbsp;\&nbsp;&nbsp;/'__`\&nbsp;
    /\&nbsp;\_\&nbsp;\&nbsp;\&nbsp;\/_/&nbsp;&nbsp;/_/\&nbsp;\_\&nbsp;\\_\&nbsp;\_\&nbsp;\&nbsp;\_/&nbsp;|/\&nbsp;&nbsp;__/&nbsp;
    \&nbsp;\___,_\&nbsp;\_\/\____\&nbsp;\____//\____\\&nbsp;\___/&nbsp;\&nbsp;\____\
    &nbsp;\/__,_&nbsp;/\/_/\/____/\/___/&nbsp;\/____/&nbsp;\/__/&nbsp;&nbsp;&nbsp;\/____/
    &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&n bsp;&nbsp;I&nbsp;have&nbsp;a&nbsp;BAD&nbsp;figlet& nbsp;addiction.

  15. #15
    Curious Noob
    Guest

    Talking Thanks All!

    Thank-you for all yer help on this project. Its been greatly appreciated.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. A very long list of questions... maybe to long...
    By Ravens'sWrath in forum C Programming
    Replies: 16
    Last Post: 05-16-2007, 05:36 AM
  2. Several Questions, main one is about protected memory
    By Tron 9000 in forum C Programming
    Replies: 3
    Last Post: 06-02-2005, 07:42 AM
  3. Trivial questions - what to do?
    By Aerie in forum A Brief History of Cprogramming.com
    Replies: 23
    Last Post: 12-26-2004, 09:44 AM
  4. questions questions questions.....
    By mfc2themax in forum A Brief History of Cprogramming.com
    Replies: 1
    Last Post: 08-14-2001, 07:22 AM