strcat and three strings

A

alternativa

Hello,
I'm dealing with a problem concerning strcat.. I would like to
concatenate 3 strings, which in fact are integer numbers.

My idea is to write something like this:


void main (void);
{
long year; /*they must be of a long type because of */
long month; /*some other reasons*/
long day;
char *str1;
char *str2;

/*here I have functions asking for the data - year, month, day*/

str1 = strcat (year,month);
str2 = strcat (str1,day);
}

So for input:
2005
01
07
I'd like to get: 20050107.
Do you think it would be possible to obtain 2005-01-07? I guess I'd
need to concanate also the year with '-', then 'year-' with 'month',
and the same with day, so I get 'year-month-day'.
thanks,
a.
 
D

Default User

alternativa said:
Hello,
I'm dealing with a problem concerning strcat.. I would like to
concatenate 3 strings, which in fact are integer numbers.

My idea is to write something like this:


void main (void);
{
long year; /*they must be of a long type because of */
long month; /*some other reasons*/
long day;
char *str1;
char *str2;

/*here I have functions asking for the data - year, month, day*/

str1 = strcat (year,month);

You need some other function besides strcat(), which you know if you
had checked its specs. It doesn't take longs, it takes pointers to
existing strings.

So for input:
2005
01
07
I'd like to get: 20050107.
Do you think it would be possible to obtain 2005-01-07? I guess I'd
need to concanate also the year with '-', then 'year-' with 'month',
and the same with day, so I get 'year-month-day'.
thanks,
a.

Forget concatenation. What you need is a formatted output function that
writes to strings. Happily there is one, sprintf().

Example:

/* this string is long enough to hold the described values. You should
check that the values fall with valid ranges before trying to format
the string, otherwise undefined behavior may result!!! */

char str[11];

sprintf(str, "%ld-%02ld-%02ld", year, month, day);


Brian
 

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,770
Messages
2,569,584
Members
45,079
Latest member
ElidaWarin

Latest Threads

Top