arrays

S

slickn_sly

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
 
T

Thomas Matthews

slickn_sly said:
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
 
J

Jason

slickn_sly said:
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
 
E

Eric Sosman

slickn_sly said:
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.
 
C

Christopher Benson-Manica

Eric Sosman said:
#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 ;)
 
C

Charles Mills

Christopher said:
Eric Sosman said:
#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
 
D

Dave Thompson

Eric Sosman said:
#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
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,767
Messages
2,569,572
Members
45,045
Latest member
DRCM

Latest Threads

Top