Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C Programming > arrays

Reply
Thread Tools

arrays

 
 
slickn_sly
Guest
Posts: n/a
 
      03-31-2005
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
 
Reply With Quote
 
 
 
 
Thomas Matthews
Guest
Posts: n/a
 
      03-31-2005
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
 
Reply With Quote
 
 
 
 
Jason
Guest
Posts: n/a
 
      03-31-2005

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

 
Reply With Quote
 
Eric Sosman
Guest
Posts: n/a
 
      03-31-2005


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.

--
http://www.velocityreviews.com/forums/(E-Mail Removed)

 
Reply With Quote
 
dot@dot.dot
Guest
Posts: n/a
 
      03-31-2005
On 31 Mar 2005 09:53:31 -0600, http://www.velocityreviews.com/forums/(E-Mail Removed)-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.


 
Reply With Quote
 
Christopher Benson-Manica
Guest
Posts: n/a
 
      03-31-2005
Eric Sosman <(E-Mail Removed)> 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.
 
Reply With Quote
 
Charles Mills
Guest
Posts: n/a
 
      03-31-2005

Christopher Benson-Manica wrote:
> Eric Sosman <(E-Mail Removed)> 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

 
Reply With Quote
 
Dave Thompson
Guest
Posts: n/a
 
      04-09-2005
On Thu, 31 Mar 2005 19:08:19 +0000 (UTC), Christopher Benson-Manica
<(E-Mail Removed)> wrote:

> Eric Sosman <(E-Mail Removed)> 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
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Multidimensional arrays and arrays of arrays Philipp Java 21 01-20-2009 08:33 AM
char arrays and integer arrays... why the difference? Bill Reyn C++ 3 06-22-2004 12:01 PM
Arrays.asList() returning java.util.Arrays$ArrayList Alexandra Stehman Java 5 06-17-2004 06:04 PM
Arrays and Pointers to Arrays kelvSYC C Programming 2 09-26-2003 06:52 AM
initializing arrays of arrays Mantorok Redgormor C Programming 4 09-11-2003 02:08 AM



Advertisments