arrays

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • slickn_sly

    #1

    arrays

    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 **
    ----------------------------------------------------------
    Best Usenet Service Providers 2026 ranked by Newsgroup Access Newsservers, Usenet Search, Features & Free Trial. Add VPN for privacy.

  • Thomas Matthews

    #2
    Re: arrays

    slickn_sly wrote:
    [color=blue]
    > 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?[/color]
    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:

    C++ Faq: http://www.parashift.com/c++-faq-lite
    C Faq: http://www.eskimo.com/~scs/c-faq/top.html
    alt.comp.lang.l earn.c-c++ faq:

    Other sites:
    http://www.josuttis.com -- C++ STL Library book
    http://www.sgi.com/tech/stl -- Standard Template Library

    Comment

    • Jason

      #3
      Re: arrays


      slickn_sly wrote:[color=blue]
      > 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[/color]

      Read about strcat in <string.h> and sprintf in <stdio.h>.

      Comment

      • Eric Sosman

        #4
        Re: arrays



        slickn_sly wrote:[color=blue]
        > 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?[/color]

        #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@sun .com

        Comment

        • Christopher Benson-Manica

          #5
          Re: arrays

          Eric Sosman <eric.sosman@su n.com> spoke thus:
          [color=blue]
          > #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);[/color]

          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(ST RING1)-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)cybers pace.org | don't, I need to know. Flames welcome.

          Comment

          • Charles Mills

            #6
            Re: arrays


            Christopher Benson-Manica wrote:[color=blue]
            > Eric Sosman <eric.sosman@su n.com> spoke thus:
            >[color=green]
            > > #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);[/color]
            >
            > 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(ST RING1)-1, "%c" STRING2, '\t' );
            >
            > which is either correct or an opportunity for me to learn something[/color]
            ;)[color=blue]
            >[/color]
            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

            Comment

            • Dave Thompson

              #7
              Re: arrays

              On Thu, 31 Mar 2005 19:08:19 +0000 (UTC), Christopher Benson-Manica
              <ataru@nospam.c yberspace.org> wrote:
              [color=blue]
              > Eric Sosman <eric.sosman@su n.com> spoke thus:
              >[color=green]
              > > #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);[/color]
              >
              > 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(ST RING1)-1, "%c" STRING2, '\t' );
              >
              > which is either correct or an opportunity for me to learn something ;)[/color]

              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.ne t

              Comment

              Working...