fill space

Discussion in 'C Programming' started by JC, Sep 27, 2003.

  1. JC

    JC Guest

    sorry . i got one more problem
    i got a string with 4 char. i want to put that in a string with 26 char. how
    can i fill space on the remain char.. ??
    is that i need to do a while loop do fill the space for the string?

    please help!

    thanks
    Jack
     
    JC, Sep 27, 2003
    #1
    1. Advertising

  2. JC

    dis Guest

    "JC" <> wrote in message news:bl3jf4$-cable.com...

    > i got a string with 4 char. i want to put that in a string with 26 char.

    how
    > can i fill space on the remain char.. ??
    > is that i need to do a while loop do fill the space for the string?


    Not sure what you want exactly, but the following program might be a good
    start.


    #include <string.h>

    int main(void)
    {
    char small[] = "abc"; /* string consisting of 4 characters */
    char large[26] = ""; /* string consisting of 26 characters */

    /* copy the string small into large, the terminating '\0' excluded */
    memcpy(large, small, sizeof small - 1);
    /* set the remaining chararacters in large to '-' */
    memset(large + sizeof small - 1, '-', sizeof large - sizeof small);
    /* terminate large with a '\0' to make it a string */
    large[sizeof large - 1] = 0;

    /* large contains now the string "abc----------------------" */

    return 0;
    }
     
    dis, Sep 27, 2003
    #2
    1. Advertising

  3. In article <bl3jf4$-cable.com>, JC wrote:
    > sorry . i got one more problem
    > i got a string with 4 char. i want to put that in a string with 26 char. how
    > can i fill space on the remain char.. ??
    > is that i need to do a while loop do fill the space for the string?


    #include <string.h>

    /* ... */

    char shorty[] = "foo"; /* four chars, including termination */
    char lengthy[26] = { 0 };

    /* ... */

    strcpy(lengthy, shorty);



    The string terminator at shorty[3] will be copied over to the
    lenthy string, terminating it at lengthy[3]. You don't need to
    "fill out" the string with anything.

    If you *do* want to fill the remainder of the string (elements
    with index 4 through to 25), then memset() will do this for you:

    memset(&(lengthy[4]), '?', 21);
    lengthy[25] = '\0';

    A for-loop will do the same thing:

    int i;
    for (i = 4; i < 25; ++i) {
    lengthy = '?';
    }
    lengthy[25] = '\0';

    The string will still be terminated at index 3 though, unless
    you overwrite the '\0' at that position with something else.

    --
    Andreas Kähäri
     
    Andreas Kahari, Sep 27, 2003
    #3
  4. "JC" <> wrote:

    >sorry . i got one more problem
    >i got a string with 4 char. i want to put that in a string with 26 char. how
    >can i fill space on the remain char.. ??
    >is that i need to do a while loop do fill the space for the string?
    >


    No need to double-post, I was already at it :p!

    ANTI-SPOIL-DISCLAIMER: If this is homework, stop reading RIGHT NOW! ;-)

    ..
    ..
    ..
    ..
    ..
    ..
    ..
    ..
    ..
    ..
    ..
    ..
    ..
    ..
    ..
    ..
    ..
    ..
    ..
    ..
    ..
    ..
    ..
    ..
    ..
    ..
    ..
    ..
    ..
    ..
    ..
    ..
    ..
    ..
    ..

    The following should work (any corrections/suggestions are welcome).
    [Note: for pre-C99 drop the restrict keyword]

    /*------------------8<----------------*/
    #include <stdio.h>
    #include <stdlib.h>

    /*
    ** Prototype:
    */
    char *Strncpypad( char * restrict s1, const char * restrict s2,
    size_t n, int pad_char );

    /*
    ** Simple test:
    */

    #define BUFLEN 27

    int main( void )
    {
    char s[ BUFLEN ] = "This is a garbage string!!";
    char t[] = "abcd";

    printf( "s before: '%s'\n", s );
    printf( "t before: '%s'\n", t );
    Strncpypad( s, t, BUFLEN, ' ' );
    printf( "s after : '%s'\n", s );

    return EXIT_SUCCESS;
    }


    /*
    ** Strncpypad
    **
    ** Copy up to n-1 characters from the string pointed to by s2 to the
    ** array pointed to by s1. Characters that follow a null character
    ** are not copied. The result will be padded with pad_char, if
    ** applicable, and will always be null-terminated.
    */

    char *Strncpypad( char * restrict s1, const char * restrict s2,
    size_t buflen, int pad_char )
    {
    size_t i;

    for ( i = 0; i < buflen-1; i++ )
    {
    if ( *s2 )
    s1[ i ] = *s2++;
    else
    s1[ i ] = pad_char;
    }
    s1[ i ] = '\0';

    return s1;
    }
    /*------------------8<----------------*/


    Regards

    Irrwahn
    --
    Computer: a million morons working at the speed of light.
     
    Irrwahn Grausewitz, Sep 27, 2003
    #4
  5. JC

    JC Guest

    thanks.
    this is very useful coding.
    "dis" <> wrote in message
    news:bl3mjd$lvb$1.nb.home.nl...
    > "JC" <> wrote in message news:bl3jf4$-cable.com...
    >
    > > i got a string with 4 char. i want to put that in a string with 26 char.

    > how
    > > can i fill space on the remain char.. ??
    > > is that i need to do a while loop do fill the space for the string?

    >
    > Not sure what you want exactly, but the following program might be a good
    > start.
    >
    >
    > #include <string.h>
    >
    > int main(void)
    > {
    > char small[] = "abc"; /* string consisting of 4 characters */
    > char large[26] = ""; /* string consisting of 26 characters */
    >
    > /* copy the string small into large, the terminating '\0' excluded */
    > memcpy(large, small, sizeof small - 1);
    > /* set the remaining chararacters in large to '-' */
    > memset(large + sizeof small - 1, '-', sizeof large - sizeof small);
    > /* terminate large with a '\0' to make it a string */
    > large[sizeof large - 1] = 0;
    >
    > /* large contains now the string "abc----------------------" */
    >
    > return 0;
    > }
    >
    >
    >
     
    JC, Sep 27, 2003
    #5
  6. "dis" <> wrote:

    >"JC" <> wrote in message news:bl3jf4$-cable.com...
    >
    >> i got a string with 4 char. i want to put that in a string with 26 char.

    >how
    >> can i fill space on the remain char.. ??
    >> is that i need to do a while loop do fill the space for the string?

    >
    >Not sure what you want exactly, but the following program might be a good
    >start.
    >
    >
    >#include <string.h>
    >
    >int main(void)
    >{
    > char small[] = "abc"; /* string consisting of 4 characters */
    > char large[26] = ""; /* string consisting of 26 characters */
    >
    > /* copy the string small into large, the terminating '\0' excluded */
    > memcpy(large, small, sizeof small - 1);


    Fails if small was dynamically allocated.
    No protection against buffer overrun.

    > /* set the remaining chararacters in large to '-' */
    > memset(large + sizeof small - 1, '-', sizeof large - sizeof small);


    Fails if small and/or large were dynamically allocated.
    Again, no protection against buffer overrun.

    > /* terminate large with a '\0' to make it a string */
    > large[sizeof large - 1] = 0;
    >
    > /* large contains now the string "abc----------------------" */
    >
    > return 0;
    >}
    >


    Regards

    Irrwahn
    --
    Computer: a million morons working at the speed of light.
     
    Irrwahn Grausewitz, Sep 27, 2003
    #6
  7. Andreas Kahari <> wrote:

    >In article <bl3jf4$-cable.com>, JC wrote:
    >> sorry . i got one more problem
    >> i got a string with 4 char. i want to put that in a string with 26 char. how
    >> can i fill space on the remain char.. ??
    >> is that i need to do a while loop do fill the space for the string?

    >
    >#include <string.h>
    >
    >/* ... */
    >
    >char shorty[] = "foo"; /* four chars, including termination */
    >char lengthy[26] = { 0 };
    >
    >/* ... */
    >
    >strcpy(lengthy, shorty);
    >
    >
    >
    >The string terminator at shorty[3] will be copied over to the
    >lenthy string, terminating it at lengthy[3]. You don't need to
    >"fill out" the string with anything.
    >
    >If you *do* want to fill the remainder of the string (elements
    >with index 4 through to 25), then memset() will do this for you:
    >
    > memset(&(lengthy[4]), '?', 21);


    No offense intended, but:

    Magic number 4.
    Magic number 21.

    > lengthy[25] = '\0';


    Magic number 25.

    >
    >A for-loop will do the same thing:
    >
    > int i;
    > for (i = 4; i < 25; ++i) {


    Magic number 25.

    > lengthy = '?';
    > }
    > lengthy[25] = '\0';


    Magic number 21.

    >
    >The string will still be terminated at index 3 though, unless
    >you overwrite the '\0' at that position with something else.


    Regards

    Irrwahn
    --
    Computer: a million morons working at the speed of light.
     
    Irrwahn Grausewitz, Sep 27, 2003
    #7
  8. JC

    JC Guest

    sorry. my problem is
    char a[26];
    char b[26];

    now a is "abcdefg" i want to put that into b with space after "g"

    how can i fill the space in that b? or just keep in "a" with space ?
    "dis" <> wrote in message
    news:bl3mjd$lvb$1.nb.home.nl...
    > "JC" <> wrote in message news:bl3jf4$-cable.com...
    >
    > > i got a string with 4 char. i want to put that in a string with 26 char.

    > how
    > > can i fill space on the remain char.. ??
    > > is that i need to do a while loop do fill the space for the string?

    >
    > Not sure what you want exactly, but the following program might be a good
    > start.
    >
    >
    > #include <string.h>
    >
    > int main(void)
    > {
    > char small[] = "abc"; /* string consisting of 4 characters */
    > char large[26] = ""; /* string consisting of 26 characters */
    >
    > /* copy the string small into large, the terminating '\0' excluded */
    > memcpy(large, small, sizeof small - 1);
    > /* set the remaining chararacters in large to '-' */
    > memset(large + sizeof small - 1, '-', sizeof large - sizeof small);
    > /* terminate large with a '\0' to make it a string */
    > large[sizeof large - 1] = 0;
    >
    > /* large contains now the string "abc----------------------" */
    >
    > return 0;
    > }
    >
    >
    >
     
    JC, Sep 27, 2003
    #8
  9. JC wrote:

    > sorry . i got one more problem
    > i got a string with 4 char. i want to put that in a string with 26 char. how
    > can i fill space on the remain char.. ??
    > is that i need to do a while loop do fill the space for the string?
    >
    > please help!
    >
    > thanks
    > Jack


    You're welcome, Jack. Thanks for the question.

    Does this program do what you want?


    #include <stdio.h>
    #include <string.h>

    int main()
    {
    char little_string[] = "abcd";
    char bigger_string[31] = "12345678901234567890123456";

    strcat(bigger_string, little_string);

    printf("%s\n", bigger_string);

    return 0;
    }

    --Steve
     
    Steve Zimmerman, Sep 27, 2003
    #9
  10. JC wrote:

    > sorry. my problem is
    > char a[26];
    > char b[26];
    >
    > now a is "abcdefg" i want to put that into b with space after "g"


    #include <stdio.h>
    #include <string.h>

    int main()
    {
    char a[26] = "abcdefg";
    char b[26];

    strcpy(b, strcat(a, " "));

    printf("%s :Before this colon is string b\n", b);

    return 0;
    }
     
    Steve Zimmerman, Sep 27, 2003
    #10
  11. JC

    dis Guest

    "JC" <> wrote in message news:bl3p27$-cable.com...

    > sorry. my problem is
    > char a[26];
    > char b[26];
    >
    > now a is "abcdefg" i want to put that into b with space after "g"
    >
    > how can i fill the space in that b? or just keep in "a" with space ?


    The program provided in my previous post to this thread should suffice to
    answer your question the way I interpret it. Maybe you could clarify your
    question by posting some actual code illustrating your problem, and clearly
    specifying what you expect the resulting string to be.

    [snip]
     
    dis, Sep 27, 2003
    #11
  12. JC

    JC Guest

    the problem i met is i need to put a short string in to a file which provide
    a field for that string is 26 char. but i am not sure.. how many char did
    the user input. therefore i need to know how to check the size of data that
    input by user and fill space to the rest of that string.. than i can put the
    string to the file.. i need to keep the file tiny.

    Thanks

    JC

    "dis" <> wrote in message
    news:bl3s96$gpg$1.nb.home.nl...
    > "JC" <> wrote in message news:bl3p27$-cable.com...
    >
    > > sorry. my problem is
    > > char a[26];
    > > char b[26];
    > >
    > > now a is "abcdefg" i want to put that into b with space after "g"
    > >
    > > how can i fill the space in that b? or just keep in "a" with space ?

    >
    > The program provided in my previous post to this thread should suffice to
    > answer your question the way I interpret it. Maybe you could clarify your
    > question by posting some actual code illustrating your problem, and

    clearly
    > specifying what you expect the resulting string to be.
    >
    > [snip]
    >
    >
    >
     
    JC, Sep 27, 2003
    #12
  13. In article <>, Irrwahn Grausewitz wrote:
    > Andreas Kahari <> wrote:
    >

    [cut]
    >> memset(&(lengthy[4]), '?', 21);

    >
    > No offense intended, but:
    >
    > Magic number 4.
    > Magic number 21.

    [other magic numbers that could easily be computed from the
    length of the strings involved snipped]

    You're absolutely right. I didn't think about that when I was
    writing the code and I didn't catch it when reading it through.
    Thanks for noticing it.

    Andreas

    --
    Andreas Kähäri
     
    Andreas Kahari, Sep 27, 2003
    #13
  14. JC

    Mike Wahler Guest

    "JC" <> wrote in message news:bl3jf4$-cable.com...
    > sorry . i got one more problem
    > i got a string with 4 char. i want to put that in a string with 26 char.

    how
    > can i fill space on the remain char.. ??
    > is that i need to do a while loop do fill the space for the string?
    >
    > please help!


    You asked this same question in your thread
    "strcpy and strcat problem", and I answered
    that post with an example today.

    -Mike
     
    Mike Wahler, Sep 27, 2003
    #14
  15. JC

    Mike Wahler Guest

    "Steve Zimmerman" <> wrote in message
    news:...
    > JC wrote:
    >
    > > sorry. my problem is
    > > char a[26];
    > > char b[26];
    > >
    > > now a is "abcdefg" i want to put that into b with space after "g"

    >
    > #include <stdio.h>
    > #include <string.h>
    >
    > int main()
    > {
    > char a[26] = "abcdefg";
    > char b[26];
    >
    > strcpy(b, strcat(a, " "));


    You're not a real programmer, are you? I surely hope not.

    -Mike
     
    Mike Wahler, Sep 27, 2003
    #15
  16. Steve Zimmerman <> wrote:

    >JC wrote:
    >
    > > sorry . i got one more problem
    > > i got a string with 4 char. i want to put that in a string with 26 char. how
    > > can i fill space on the remain char.. ??
    > > is that i need to do a while loop do fill the space for the string?
    > >
    > > please help!
    > >
    > > thanks
    > > Jack

    >
    >You're welcome, Jack. Thanks for the question.
    >
    >Does this program do what you want?
    >
    >
    >#include <stdio.h>
    >#include <string.h>
    >
    >int main()
    >{
    > char little_string[] = "abcd";
    > char bigger_string[31] = "12345678901234567890123456";
    >
    > strcat(bigger_string, little_string);


    How does this relate to the OP's problem in any way?

    <SNIP>

    Irrwahn
    --
    Great minds run in great circles.
     
    Irrwahn Grausewitz, Sep 27, 2003
    #16
  17. JC

    JC Guest

    i am not a programmer.


    "Mike Wahler" <> wrote in message
    news:8Ekdb.6479$...
    > "Steve Zimmerman" <> wrote in message
    > news:...
    > > JC wrote:
    > >
    > > > sorry. my problem is
    > > > char a[26];
    > > > char b[26];
    > > >
    > > > now a is "abcdefg" i want to put that into b with space after "g"

    > >
    > > #include <stdio.h>
    > > #include <string.h>
    > >
    > > int main()
    > > {
    > > char a[26] = "abcdefg";
    > > char b[26];
    > >
    > > strcpy(b, strcat(a, " "));

    >
    > You're not a real programmer, are you? I surely hope not.
    >
    > -Mike
    >
    >
     
    JC, Sep 27, 2003
    #17
  18. JC

    Mike Wahler Guest

    "JC" <> wrote in message news:bl4saa$-cable.com...
    > i am not a programmer.


    That was not addressed to you, but to Steve Zimmerman.
    Did not your news reader show the thread properly?

    From what I've seen from him so far, I recommend you
    regard his code with suspicion.

    BTW please don't top post.

    -Mike
     
    Mike Wahler, Sep 27, 2003
    #18
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. M Hedges
    Replies:
    2
    Views:
    3,812
    M Hedges
    Feb 14, 2004
  2. Shuo Xiang

    Stack space, global space, heap space

    Shuo Xiang, Jul 9, 2003, in forum: C Programming
    Replies:
    10
    Views:
    2,914
    Bryan Bullard
    Jul 11, 2003
  3. Christian Seberino
    Replies:
    21
    Views:
    1,676
    Stephen Horne
    Oct 27, 2003
  4. Ian Bicking
    Replies:
    2
    Views:
    1,024
    Steve Lamb
    Oct 23, 2003
  5. Ian Bicking
    Replies:
    2
    Views:
    730
    Michael Hudson
    Oct 24, 2003
Loading...

Share This Page