Adding leading zeros to a string.

Discussion in 'Mac Programming' started by farmerdoug, Feb 16, 2010.

  1. farmerdoug macrumors 6502a

    Joined:
    Sep 16, 2008
    #1
    Can I convert numbers from 1 to say 20 to strings from 001 to 020 without if statements?
     
  2. robbieduncan Moderator emeritus

    robbieduncan

    Joined:
    Jul 24, 2002
    Location:
    London
    #2
    Is this homework by any chance? If so say so up front. Other than that I'd say look at while statements...
     
  3. farmerdoug thread starter macrumors 6502a

    Joined:
    Sep 16, 2008
    #3
    no its not homework, and I don't want to use while statements. I want do you with some sort of string formating.
     
  4. iShater macrumors 604

    iShater

    Joined:
    Aug 13, 2002
    Location:
    Chicagoland
    #4
    Hehehehe, it does sound like homework :)

    There are formatting APIs that can help you, but the question is what is the intent of the exercise.
     
  5. robbieduncan Moderator emeritus

    robbieduncan

    Joined:
    Jul 24, 2002
    Location:
    London
    #5
    Well, OK you should have said you don't want to use while statements. It's probably slower and will take more RAM but look at NSNumberFormatter assuming you are in Cocoa (similar exists in Java and probably other library-rich environments).
     
  6. farmerdoug thread starter macrumors 6502a

    Joined:
    Sep 16, 2008
    #6
    I don't want to fill up my code with unnecessary while, if or for statements every time I have to make a file name.

    I'm in c on Xcode
     
  7. robbieduncan Moderator emeritus

    robbieduncan

    Joined:
    Jul 24, 2002
    Location:
    London
    #7
    Well you could, you know, follow the rules of good program design and make a function/method to do it that would have the while loop in it.

    In that case sprintf
     
  8. farmerdoug thread starter macrumors 6502a

    Joined:
    Sep 16, 2008
    #8
    That's still one more function in a program loaded with them. I take it the answer to my original question is either no or probably not.
     
  9. miles01110 macrumors Core

    miles01110

    Joined:
    Jul 24, 2006
    Location:
    The Ivory Tower (I'm not coming down)
    #9
    It's worse... it's actual work. farmerdoug has posted a series of threads in the programming forum in which he manages to make the rest of us shake our heads and/or try to get across some of the principles of asking for help, most of which haven't sunk in apparently.
     
  10. farmerdoug thread starter macrumors 6502a

    Joined:
    Sep 16, 2008
    #10
    I'm not sure what is language appropriate here but guys like lee, lloyd and chown have been very helpful. You can go hide under a rock.
     
  11. robbieduncan Moderator emeritus

    robbieduncan

    Joined:
    Jul 24, 2002
    Location:
    London
    #11
    Why are you trying to avoid if/while statements? They are core logic constructs and any code would be expected to be full of them. They are well understood by programmers and compilers optimise them well.

    I suggested the alternative for C above but I would not that this is much more expensive to call as it is general purpose and has to parse the format string. The only reason I can think of for avoiding these core constructs would be some amazingly constrained embedded system and in that case I doubt sprintf is available due to it's resource requirements.

    Well that's lovely. You badly state your requirements, failing to list key constraints. I work with you to figure out what you really want (as opposed to what you say you want) and then post the correct solution and all you can do is insult me. Don't expect any more help until your attitude improves.
     
  12. miles01110 macrumors Core

    miles01110

    Joined:
    Jul 24, 2006
    Location:
    The Ivory Tower (I'm not coming down)
    #12
    The answer to your original question is "yes." The members you identified above have indeed been extremely helpful- only after they've had to lecture you about things like posting complete code and a hands-on lesson in how to debug.

    We're not going to do your work for you. If you want to know how to add leading zeros, you're welcome to post your first stab at your problem so we can give you suggestions.
     
  13. farmerdoug thread starter macrumors 6502a

    Joined:
    Sep 16, 2008
    #13
    I didn't post my any original stab because I know how to do this with for statements, etc. I'm looking for some printf format or string routine that will this semi automatically.

    this
    sprintf(snum,"%3d", i + 1);
    strcpy(file,cube);
    strcat(file,snum);

    adds blanks.
     
  14. robbieduncan Moderator emeritus

    robbieduncan

    Joined:
    Jul 24, 2002
    Location:
    London
    #14


    I see you are choosing to not apologise for you're earlier unwarranted attack on me. Therefore I won't tell you the solution. Especially as it's in the link I posted earlier and you seem to be too lazy to read the entire document and find it.

    The ability to read and understand documentation is a core programming skill. Learn it.
     
  15. farmerdoug thread starter macrumors 6502a

    Joined:
    Sep 16, 2008
    #15
    Robbie
    Sorry, that reply was to miles. And I didn't look at the link because I thought that if I had to I would just as soon go with if statements.
     
  16. farmerdoug thread starter macrumors 6502a

    Joined:
    Sep 16, 2008
    #16
    with an if statement

    Code:
    char ** makedatasetlist( char *root, int num, int reads)
    { 
        char * cube, snum[5];
        char **file;
        int i;
        
        if ( (cube =(char *)calloc(200, sizeof(char)))== NULL)
    		printf("No memory for cube\n");
    
        if ( (file =(char **)calloc(reads - 1, sizeof(char*)))== NULL)
    		printf("No memory for filenames\n");
            for( i = 0; i < reads; i++)
                if ( (file[i] =(char *)calloc(100, sizeof(char)))== NULL)
                    printf("No memory for filenames\n");
    
            strcpy(cube,root);
            if ( num < 10)
            strcat( cube, "00");
            else
            strcat(cube,"0");
            sprintf(snum,"%d", num);
            strcat(cube,snum);
            strcat(cube,"_f");
        
        for (i = 0; i < reads - 1; i ++)
        {   
            strcpy(file[i],cube);
            if ( i < 10)
            strcat( file[i], "00");
            else
            strcat(file[i],"0");
            
            sprintf(snum,"%d", i);
            
            strcat(file[i],snum);
            strcat(file[i],".fits");
            printf("%s\n", file[i]);
        }
        
        return file;
    }    
    
    
     
  17. robbieduncan Moderator emeritus

    robbieduncan

    Joined:
    Jul 24, 2002
    Location:
    London
    #17
    There we go. The correct format string for sprintf is "%03d". I would still use a couple of if statements to protect against numbers <0 and >999 myself...
     
  18. farmerdoug thread starter macrumors 6502a

    Joined:
    Sep 16, 2008
    #18
    So I had to right code so that before you would give me a better way?
     
  19. robbieduncan Moderator emeritus

    robbieduncan

    Joined:
    Jul 24, 2002
    Location:
    London
    #19
    No, I was on a train. I would have posted that after the post above the one with lots of code. As I said it was in the link I posted to sprintf. Reading the documentation is the most powerful tool any programmer has. I honestly expected you would already have that answer by now.
     
  20. farmerdoug thread starter macrumors 6502a

    Joined:
    Sep 16, 2008
  21. robbieduncan Moderator emeritus

    robbieduncan

    Joined:
    Jul 24, 2002
    Location:
    London
    #21
    No, the one with text sprintf in post #7 in this thread.
     
  22. farmerdoug thread starter macrumors 6502a

    Joined:
    Sep 16, 2008
  23. savar macrumors 68000

    savar

    Joined:
    Jun 6, 2003
    Location:
    District of Columbia
    #23
    Kinky. And I'm flattered... but I'm just not that into you.

    Edit: We should adopt the SSCCE mantra in this forum. Its much simpler than going through this with every new poster.
     
  24. notjustjay macrumors 603

    notjustjay

    Joined:
    Sep 19, 2003
    Location:
    Canada, eh?
    #24
    Here's a fun link:

    http://www.thedailywtf.com

    The site is full of examples of what you get when you have programmers that don't want to learn to program properly.
     
  25. jared_kipe macrumors 68030

    jared_kipe

    Joined:
    Dec 8, 2003
    Location:
    Seattle
    #25
    Or we could just come to some kind of silent agreement not to help the same people(person?) with the same s*%# over and over.
     

Share This Page