Thread: Inserting blank spaces in front of a string? NEED QUICK HELP PLEASE

  1. #1
    Registered User
    Join Date
    Feb 2005
    Posts
    14

    Inserting blank spaces in front of a string? NEED QUICK HELP PLEASE

    Hello all, I'm trying to alter this code for atoi to accept an integer argument, which, if the number is larger than the length of the output string, it will insert the difference in spaces in front of the output.

    For instance, if the argument is x= 15
    and the output string is '24835'

    then the final printed output would be 15-5 characters = 10 blank spaces inserted to the left of '24835' so it would print

    24835
    ^^10 blank spaces

    Can anyone please help? Thanks in advance. Also, if someone could please run my code on their comps and verify if my atoi function is correct and gives the correct output please.


    Code:
    #include<stdio.h>
    #include<string.h>
    
    
    /* reverse:     reverse string s in place */
    void reverse(char s[])
    {
            int c,i,j;
    
            for(i = 0,j = strlen(s)-1; i < j; i++,j--)
            {
                    c = s[i];
                    s[i] = s[j];
                    s[j] = c;
            }
    }
    
    /* itoa:  convert n to characters in s */
    void itoa(int n,char s[])
    {
            int i,sign;
    
            if((sign = n) < 0)      /* record sign */
                    n = -n;                 /* make n positive */
    
            i = 0;
    
            do {                                            /* generate
    digits in reverse order */
                    s[i++] = n % 10 + '0';  /* get next digit */
            } while((n /= 10) > 0);         /* delete it */
    
            if(sign < 0)
                    s[i++] = '-';
            s[i] = '\0';
            reverse(s);
    }
    int main()
    {
            char string[50];  /* allocate array space for 50 characters */
            int x;
    
            x = 393;
            itoa(x, string);
            printf("%s\n", string);
            return 0;
    }

  2. #2
    return 0;
    Join Date
    Jan 2005
    Location
    Netherlands
    Posts
    89
    printf can do what you want:

    Code:
    printf("%10s\n",string);
    Or, the way I believe you want it:

    Code:
    int x = 10;
    printf("%*s\n",x,string);
    It'll add spaces in front of the string if it is less than x characters.
    Your program seems to be working fine.

    Btw if you'd want the itoa() function to store the spaces in your array, you could add the bold code:

    Code:
    void itoa(int n,char s[],int length)
    {
            int i,sign;
    
            if((sign = n) < 0)      /* record sign */
                    n = -n;                 /* make n positive */
    
            i = 0;
    
            do {                                            /* generate
    digits in reverse order */
                    s[i++] = n % 10 + '0';  /* get next digit */
            } while((n /= 10) > 0);         /* delete it */
    
            if(sign < 0)
                    s[i++] = '-';
    
            while(i < length)
              s[i++] = ' ';
    
            s[i] = '\0';
            reverse(s);
    }
    Last edited by Snip; 04-06-2005 at 07:27 AM.

  3. #3
    Registered User
    Join Date
    Feb 2005
    Posts
    14
    wow snip thanks so much! I didn't realize it was that easy...I kept on experimenting with strlength comparisons and even tried to implement a very complicated node pointer scheme...I'm only going on 2 hours sleep so that probably doesn't help much either haha thanks again!

  4. #4
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    Ditto
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. C++ ini file reader problems
    By guitarist809 in forum C++ Programming
    Replies: 7
    Last Post: 09-04-2008, 06:02 AM
  2. We Got _DEBUG Errors
    By Tonto in forum Windows Programming
    Replies: 5
    Last Post: 12-22-2006, 05:45 PM
  3. Message class ** Need help befor 12am tonight**
    By TransformedBG in forum C++ Programming
    Replies: 1
    Last Post: 11-29-2006, 11:03 PM
  4. Compile Error that i dont understand
    By bobthebullet990 in forum C++ Programming
    Replies: 5
    Last Post: 05-05-2006, 09:19 AM
  5. Replies: 4
    Last Post: 03-03-2006, 02:11 AM