arrays

Discussion in 'C Programming' started by slickn_sly, Mar 31, 2005.

  1. slickn_sly

    slickn_sly Guest

    I was wondering how you can add "\t" character in an array. For
    example,

    if my first input string is: my name is john
    and my second input string is: i like the beach.


    How would i insert the tab character, "\t", after john. and add the
    second
    string after the tab character?
    Posted at: http://www.groupsrv.com

    Posted Via Usenet.com Premium Usenet Newsgroup Services
    ----------------------------------------------------------
    ** SPEED ** RETENTION ** COMPLETION ** ANONYMITY **
    ----------------------------------------------------------
    http://www.usenet.com
    slickn_sly, Mar 31, 2005
    #1
    1. Advertising

  2. slickn_sly wrote:

    > I was wondering how you can add "\t" character in an array. For
    > example,
    >
    > if my first input string is: my name is john
    > and my second input string is: i like the beach.
    >
    >
    > How would i insert the tab character, "\t", after john. and add the
    > second
    > string after the tab character?

    Create another array which is larger than the sum of
    the first string plus the second string plus the tab character.

    Copy the first string into the new array, use strcpy.
    Append the tab character (concatenate it), use strcat.
    Concatenate the second string.

    See also sprintf.

    --
    Thomas Matthews

    C++ newsgroup welcome message:
    http://www.slack.net/~shiva/welcome.txt
    C++ Faq: http://www.parashift.com/c -faq-lite
    C Faq: http://www.eskimo.com/~scs/c-faq/top.html
    alt.comp.lang.learn.c-c++ faq:
    http://www.comeaucomputing.com/learn/faq/
    Other sites:
    http://www.josuttis.com -- C++ STL Library book
    http://www.sgi.com/tech/stl -- Standard Template Library
    Thomas Matthews, Mar 31, 2005
    #2
    1. Advertising

  3. slickn_sly

    Jason Guest

    slickn_sly wrote:
    > I was wondering how you can add "\t" character in an array. For
    > example,
    >
    > if my first input string is: my name is john
    > and my second input string is: i like the beach.
    >
    >
    > How would i insert the tab character, "\t", after john. and add the
    > second
    > string after the tab character?
    > Posted at: http://www.groupsrv.com
    >
    > Posted Via Usenet.com Premium Usenet Newsgroup Services
    > ----------------------------------------------------------
    > ** SPEED ** RETENTION ** COMPLETION ** ANONYMITY **
    > ----------------------------------------------------------
    > http://www.usenet.com


    Read about strcat in <string.h> and sprintf in <stdio.h>.
    Jason, Mar 31, 2005
    #3
  4. slickn_sly

    Eric Sosman Guest

    slickn_sly wrote:
    > I was wondering how you can add "\t" character in an array. For
    > example,
    >
    > if my first input string is: my name is john
    > and my second input string is: i like the beach.
    >
    >
    > How would i insert the tab character, "\t", after john. and add the
    > second
    > string after the tab character?


    #include <string.h>
    #define STRING1 "my name is john"
    #define STRING2 "i like the beach."
    ....
    char array[sizeof STRING1 + sizeof STRING2] = STRING1;
    strcat(array, "\t");
    strcat(array, STRING2);

    I feel slightly guilty about posting this even though
    it is (AFAIK) correct. Somebody is going to misunderstand
    the way array[] is sized, and I'll be partly to blame for
    the ensuing trouble.

    --
    Eric Sosman, Mar 31, 2005
    #4
  5. slickn_sly

    Guest

    On 31 Mar 2005 09:53:31 -0600, -spam.invalid
    (slickn_sly) wrote:

    >How would i insert the tab character, "\t", after john. and add the
    >second
    >string after the tab character?


    Have a look at "strcat" in your language docs.
    , Mar 31, 2005
    #5
  6. Eric Sosman <> spoke thus:

    > #include <string.h>
    > #define STRING1 "my name is john"
    > #define STRING2 "i like the beach."
    > ...
    > char array[sizeof STRING1 + sizeof STRING2] = STRING1;
    > strcat(array, "\t");
    > strcat(array, STRING2);


    It might be better to have "\t" #define'd as it's own string, since
    the space needed for array is dependent on the string passed in the
    first call to strcat(). I personally like

    char array[sizeof STRING1 + sizeof STRING2]=STRING1;
    sprintf( array+sizeof(STRING1)-1, "%c" STRING2, '\t' );

    which is either correct or an opportunity for me to learn something ;)

    --
    Christopher Benson-Manica | I *should* know what I'm talking about - if I
    ataru(at)cyberspace.org | don't, I need to know. Flames welcome.
    Christopher Benson-Manica, Mar 31, 2005
    #6
  7. Christopher Benson-Manica wrote:
    > Eric Sosman <> spoke thus:
    >
    > > #include <string.h>
    > > #define STRING1 "my name is john"
    > > #define STRING2 "i like the beach."
    > > ...
    > > char array[sizeof STRING1 + sizeof STRING2] = STRING1;
    > > strcat(array, "\t");
    > > strcat(array, STRING2);

    >
    > It might be better to have "\t" #define'd as it's own string, since
    > the space needed for array is dependent on the string passed in the
    > first call to strcat(). I personally like
    >
    > char array[sizeof STRING1 + sizeof STRING2]=STRING1;
    > sprintf( array+sizeof(STRING1)-1, "%c" STRING2, '\t' );
    >
    > which is either correct or an opportunity for me to learn something

    ;)
    >

    You could just say do this:

    static const char str[] = STRING1 "\t" STRING2;

    assuming STRING1 and STRING2 are literals, which is probably not the
    case.

    -Charlie
    Charles Mills, Mar 31, 2005
    #7
  8. On Thu, 31 Mar 2005 19:08:19 +0000 (UTC), Christopher Benson-Manica
    <> wrote:

    > Eric Sosman <> spoke thus:
    >
    > > #include <string.h>
    > > #define STRING1 "my name is john"
    > > #define STRING2 "i like the beach."
    > > ...
    > > char array[sizeof STRING1 + sizeof STRING2] = STRING1;
    > > strcat(array, "\t");
    > > strcat(array, STRING2);

    >
    > It might be better to have "\t" #define'd as it's own string, since
    > the space needed for array is dependent on the string passed in the
    > first call to strcat(). I personally like
    >
    > char array[sizeof STRING1 + sizeof STRING2]=STRING1;
    > sprintf( array+sizeof(STRING1)-1, "%c" STRING2, '\t' );
    >
    > which is either correct or an opportunity for me to learn something ;)


    It is correct for the values given, but extremely dangerous if STRING2
    is later modified (in real practice, probably by someone else) to a
    value that contains a percent sign. You could avoid that by
    sprintf ( /*as above*/, "%c%s", '\t', STRING2);

    Of course as already noted for constants you don't need code at all.

    - David.Thompson1 at worldnet.att.net
    Dave Thompson, Apr 9, 2005
    #8
    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. Alexandra Stehman
    Replies:
    5
    Views:
    30,530
    Chris Smith
    Jun 17, 2004
  2. Bill Reyn
    Replies:
    3
    Views:
    2,225
    Bob Hairgrove
    Jun 22, 2004
  3. Mantorok Redgormor

    initializing arrays of arrays

    Mantorok Redgormor, Sep 10, 2003, in forum: C Programming
    Replies:
    4
    Views:
    550
  4. kelvSYC

    Arrays and Pointers to Arrays

    kelvSYC, Sep 26, 2003, in forum: C Programming
    Replies:
    2
    Views:
    371
  5. Philipp
    Replies:
    21
    Views:
    1,101
    Philipp
    Jan 20, 2009
Loading...

Share This Page