Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C Programming > strcat problem

Reply
Thread Tools

strcat problem

 
 
ctara_shafa@yahoo.co.uk
Guest
Posts: n/a
 
      01-08-2006
Hi,
I have a following problem: I'm creating a list and one of the fields
should contain the date. Firstly I ask the user for the year, month and
day and then I'd like to collect all this data in one field. To do this
I tried to concatenate those 3 numbers into one using strcat. A piece
of code is as follows

/*function to enter the date*/
char * date (void)
{
long year; /* it must of a long type */
long month; /* because of some other reasons*/
long day;
char *str1;
char *str2;
char *str3;
char *str4;

/*here are the functions collecting the data from the input*/

str1 = strcat (year,'-');
str2 = strcat (str1, month);
str3 = strcat (str2,'-');
str4 = strcat (str3,day);

return str4;
}

I think the result should be "year-month-day".. Why it doesn't work?

 
Reply With Quote
 
 
 
 
Ico
Guest
Posts: n/a
 
      01-09-2006
http://www.velocityreviews.com/forums/(E-Mail Removed) wrote:

> I have a following problem: I'm creating a list and one of the fields
> should contain the date. Firstly I ask the user for the year, month and
> day and then I'd like to collect all this data in one field. To do this
> I tried to concatenate those 3 numbers into one using strcat. A piece
> of code is as follows


Posting your question 3 times in a row is *not* going to help you in
getting friendly, helpful answers. If your newsreader is broken, use a
better one. If you don't know how to use it properly, learn. If you
posted 3 times on purpose, don't expect people to be nice to you.

Ico

--
:wq
^X^Cy^K^X^C^C^C^C
 
Reply With Quote
 
 
 
 
abdur_rab7@yahoo.co.in
Guest
Posts: n/a
 
      01-09-2006
No it does not work as you think

(E-Mail Removed) wrote:
> Hi,
> I have a following problem: I'm creating a list and one of the fields
> should contain the date. Firstly I ask the user for the year, month and
> day and then I'd like to collect all this data in one field. To do this
> I tried to concatenate those 3 numbers into one using strcat. A piece
> of code is as follows
>
> /*function to enter the date*/
> char * date (void)
> {
> long year; /* it must of a long type */
> long month; /* because of some other reasons*/
> long day;
> char *str1;
> char *str2;
> char *str3;
> char *str4;
>
> /*here are the functions collecting the data from the input*/
>
> str1 = strcat (year,'-');
> str2 = strcat (str1, month);


The Syntax of strcat is

char *strcat(char *s1, const char *s2);

The strcat() function appends a copy of the string pointed to by s2
(including the terminating null character) to the end of the string
pointed to by s1. The initial character of s2 overwrites the null
character at the end of s1. If copying occurs between objects that
overlap, the behavior is undefined.

The function strcat() does not allocate any storage. The caller must
insure that the buffer pointed to by s1 is long enough for string s2
and its terminating null character.

is year a char* ???

> str3 = strcat (str2,'-');
> str4 = strcat (str3,day);
>


Where is the memory allocation for str1, str2, str3 and str4 ???

> return str4;
> }
>
> I think the result should be "year-month-day".. Why it doesn't work?


use sprintf to do that
The below code would work as you thought

char * date (void)
{
long year; /* it must of a long type */
long month; /* because of some other reasons*/
long day;

char caBuffer[4]; /* used for conversion */

char* str1;

str1 = (char*) malloc (11);
sprintf (caBuffer, "%lu", year);
strcpy (str1, year);
strcat ((str1,'-');
sprintf (caBuffer, "%lu", month);
strcat (str1, month);
strcat (str1,'-');
sprintf (caBuffer, "%lu", day);
strcat (str1, day);

return str1;
}

Best Regards,
Abdur

 
Reply With Quote
 
abdur_rab7@yahoo.co.in
Guest
Posts: n/a
 
      01-09-2006

(E-Mail Removed) wrote:
> No it does not work as you think
>
> (E-Mail Removed) wrote:
> > Hi,
> > I have a following problem: I'm creating a list and one of the fields
> > should contain the date. Firstly I ask the user for the year, month and
> > day and then I'd like to collect all this data in one field. To do this
> > I tried to concatenate those 3 numbers into one using strcat. A piece
> > of code is as follows
> >
> > /*function to enter the date*/
> > char * date (void)
> > {
> > long year; /* it must of a long type */
> > long month; /* because of some other reasons*/
> > long day;
> > char *str1;
> > char *str2;
> > char *str3;
> > char *str4;
> >
> > /*here are the functions collecting the data from the input*/
> >
> > str1 = strcat (year,'-');
> > str2 = strcat (str1, month);

>
> The Syntax of strcat is
>
> char *strcat(char *s1, const char *s2);
>
> The strcat() function appends a copy of the string pointed to by s2
> (including the terminating null character) to the end of the string
> pointed to by s1. The initial character of s2 overwrites the null
> character at the end of s1. If copying occurs between objects that
> overlap, the behavior is undefined.
>
> The function strcat() does not allocate any storage. The caller must
> insure that the buffer pointed to by s1 is long enough for string s2
> and its terminating null character.
>
> is year a char* ???
>
> > str3 = strcat (str2,'-');
> > str4 = strcat (str3,day);
> >

>
> Where is the memory allocation for str1, str2, str3 and str4 ???
>
> > return str4;
> > }
> >
> > I think the result should be "year-month-day".. Why it doesn't work?

>
> use sprintf to do that
> The below code would work as you thought
>
> char * date (void)
> {
> long year; /* it must of a long type */
> long month; /* because of some other reasons*/
> long day;
>
> char caBuffer[4]; /* used for conversion */
>
> char* str1;
>
> str1 = (char*) malloc (11);
> sprintf (caBuffer, "%lu", year);
> strcpy (str1, year);
> strcat ((str1,'-');
> sprintf (caBuffer, "%lu", month);
> strcat (str1, month);
> strcat (str1,'-');
> sprintf (caBuffer, "%lu", day);
> strcat (str1, day);
>
> return str1;
> }
>
> Best Regards,
> Abdur


I am sorry, a small correction in the programm

char * date (void)
{
long year; /* it must of a long type */
long month; /* because of some other reasons*/
long day;

char caBuffer[4]; /* used for conversion */

char* str1;

str1 = (char*) malloc (11);
sprintf (caBuffer, "%lu", year);
strcpy (str1, (caBuffer);
strcat ((str1,'-');
sprintf (caBuffer, "%lu", month);
strcat (str1, (caBuffer);
strcat (str1,'-');
sprintf (caBuffer, "%lu", day);
strcat (str1, (caBuffer);


return str1;
}

Best Regards,
Abdur

 
Reply With Quote
 
abdur_rab7@yahoo.co.in
Guest
Posts: n/a
 
      01-09-2006

(E-Mail Removed) wrote:
> No it does not work as you think
>
> (E-Mail Removed) wrote:
> > Hi,
> > I have a following problem: I'm creating a list and one of the fields
> > should contain the date. Firstly I ask the user for the year, month and
> > day and then I'd like to collect all this data in one field. To do this
> > I tried to concatenate those 3 numbers into one using strcat. A piece
> > of code is as follows
> >
> > /*function to enter the date*/
> > char * date (void)
> > {
> > long year; /* it must of a long type */
> > long month; /* because of some other reasons*/
> > long day;
> > char *str1;
> > char *str2;
> > char *str3;
> > char *str4;
> >
> > /*here are the functions collecting the data from the input*/
> >
> > str1 = strcat (year,'-');
> > str2 = strcat (str1, month);

>
> The Syntax of strcat is
>
> char *strcat(char *s1, const char *s2);
>
> The strcat() function appends a copy of the string pointed to by s2
> (including the terminating null character) to the end of the string
> pointed to by s1. The initial character of s2 overwrites the null
> character at the end of s1. If copying occurs between objects that
> overlap, the behavior is undefined.
>
> The function strcat() does not allocate any storage. The caller must
> insure that the buffer pointed to by s1 is long enough for string s2
> and its terminating null character.
>
> is year a char* ???
>
> > str3 = strcat (str2,'-');
> > str4 = strcat (str3,day);
> >

>
> Where is the memory allocation for str1, str2, str3 and str4 ???
>
> > return str4;
> > }
> >
> > I think the result should be "year-month-day".. Why it doesn't work?

>
> use sprintf to do that
> The below code would work as you thought
>
> char * date (void)
> {
> long year; /* it must of a long type */
> long month; /* because of some other reasons*/
> long day;
>
> char caBuffer[4]; /* used for conversion */
>
> char* str1;
>
> str1 = (char*) malloc (11);
> sprintf (caBuffer, "%lu", year);
> strcpy (str1, year);
> strcat ((str1,'-');
> sprintf (caBuffer, "%lu", month);
> strcat (str1, month);
> strcat (str1,'-');
> sprintf (caBuffer, "%lu", day);
> strcat (str1, day);
>
> return str1;
> }
>
> Best Regards,
> Abdur


I am sorry, a small correction in the programm

char * date (void)
{
long year; /* it must of a long type */
long month; /* because of some other reasons*/
long day;

char caBuffer[4]; /* used for conversion */

char* str1;

str1 = (char*) malloc (11);
sprintf (caBuffer, "%lu", year);
strcpy (str1, caBuffer);
strcat (str1,'-');
sprintf (caBuffer, "%lu", month);
strcat (str1, caBuffer);
strcat (str1,'-');
sprintf (caBuffer, "%lu", day);
strcat (str1, caBuffer);


return str1;
}

Best Regards,
Abdur

 
Reply With Quote
 
Ico
Guest
Posts: n/a
 
      01-09-2006
(E-Mail Removed) <(E-Mail Removed)> wrote:

[..]

> Best Regards,
> Abdur


Is it my reader that is broken, or is everybody posting twice or thrice
today ?

--
:wq
^X^Cy^K^X^C^C^C^C
 
Reply With Quote
 
abdur_rab7@yahoo.co.in
Guest
Posts: n/a
 
      01-09-2006

Ico wrote:
> (E-Mail Removed) <(E-Mail Removed)> wrote:
>
> [..]
>
> > Best Regards,
> > Abdur

>
> Is it my reader that is broken, or is everybody posting twice or thrice
> today ?
>
> --
> :wq
> ^X^Cy^K^X^C^C^C^C


I hope, it has happened by mistake. I am sorry about it

Best Regards,
Abdur

 
Reply With Quote
 
M.B
Guest
Posts: n/a
 
      01-09-2006

(E-Mail Removed) wrote:
> Hi,
> I have a following problem: I'm creating a list and one of the fields
> should contain the date. Firstly I ask the user for the year, month and
> day and then I'd like to collect all this data in one field. To do this
> I tried to concatenate those 3 numbers into one using strcat. A piece
> of code is as follows
>
> /*function to enter the date*/
> char * date (void)
> {
> long year; /* it must of a long type */
> long month; /* because of some other reasons*/
> long day;
> char *str1;
> char *str2;
> char *str3;
> char *str4;
>
> /*here are the functions collecting the data from the input*/
>


> str1 = strcat (year,'-');


No memory allocated for str1,str2,str3,str4

> str2 = strcat (str1, month);
> str3 = strcat (str2,'-');
> str4 = strcat (str3,day);
>

in any case syntax for strcat is
char *strcat(char *,char *)
'-' does not work here "-" does.

> return str4;
> }
>
> I think the result should be "year-month-day".. Why it doesn't work?


 
Reply With Quote
 
Keith Thompson
Guest
Posts: n/a
 
      01-09-2006
Ico <(E-Mail Removed)> writes:
> (E-Mail Removed) <(E-Mail Removed)> wrote:
>
> [..]
>
>> Best Regards,
>> Abdur

>
> Is it my reader that is broken, or is everybody posting twice or thrice
> today ?


I think groups.google.com is now broken in yet another way. Whatever
the problem is, it's new (and presumably not the posters' fault).

--
Keith Thompson (The_Other_Keith) (E-Mail Removed) <http://www.ghoti.net/~kst>
San Diego Supercomputer Center <*> <http://users.sdsc.edu/~kst>
We must do something. This is something. Therefore, we must do this.
 
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
strcat problem alternativa C Programming 5 01-09-2006 05:53 PM
Problem with strcat, strcpy,sprintf diego.arias.vel@gmail.com C Programming 24 10-30-2005 11:16 PM
strcpy and strcat problem JC C Programming 23 09-29-2003 04:27 PM
strcat problem again Ian Stanley C Programming 18 09-22-2003 06:06 AM
strcat problem Ian Stanley C Programming 5 09-13-2003 02:03 PM



Advertisments