473,836 Members | 1,421 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

strcpy and strcat problem

JC
hi,

i want to combine two string together.. and put in to another string. how
can i do . i try myself.. with the follow code. but seem can't get the
result i want.. i want to get the result with "c is abcd" .
#include <stdio.h>
#include <string.h>
void main() {
char a[2]="ab";
char b[2]="cd";
char c[4]=" \0";
strcpy(c,a);
strcat(c,b);
printf("a is %s\n",a);
printf("b is %s\n",b);
printf("c is %s\n",c);
}

i only get this result

a is b?
b is b?
c is ab?b?

what problem to my coding? anything wrong?
please help!!.

Thanks

JC

ps. if i use strcpy(c,"ab"); and strcat(c,"cd"); i can get the result.."c is
abcd"
Nov 13 '05
23 11625
Arthur J. O'Dwyer wrote:

On Sat, 27 Sep 2003, Steve Zimmerman wrote:

JC,

Thank you for your question.

#include <stdio.h>
#include <string.h>

int main()
{
char a[] = "ab";
char b[] = "cd";

char c[5];

strcpy(c, a); /* Instead of these two lines, you may write */
strcat(c, b); /* strcpy(c, strcat(a, b)); */
I seem to recall having said this before, but:

STOP STOP STOP STOP STOP STOP STOP STOP STOP!!

Do *NOT* give newbies incorrect information!
It's bad!
Don't do it!
Resist the temptation!
Pick up a book and learn how to answer questions correctly!
Thank you!


It appears that you're wasting your time, Arthur. Just about every Zimmerman
article I read is flawed in some major respect, and he seems to ignore
corrections. You might as well just address the corrections directly to the
OP...
[The "Instead of..." comment is utterly wrong; the code
suggested won't even "work" on typical x86 implementations --
so it's fairly obvious that the comment was the product of
either willful ignorance or maliciousness.]


....like this, only perhaps in more detail.

--
Richard Heathfield : bi****@eton.pow ernet.co.uk
"Usenet is a strange place." - Dennis M Ritchie, 29 July 1999.
C FAQ: http://www.eskimo.com/~scs/C-faq/top.html
K&R answers, C books, etc: http://users.powernet.co.uk/eton
Nov 13 '05 #11
Irrwahn Grausewitz wrote:
CBFalconer <cb********@yah oo.com> wrote:
JC wrote:

<SNIP>
char c[4]=" \0";


I'm not sure what this is, but it isn't a string either.

To me it looks like it is a string consisting of three blanks
followed by a null character, inititialized invoking undefined
behaviour by writing beyond array bounds.


No, the string literal comprises "whatever is in quotes" plus a null
terminating character. If "whatever is in quotes" comes to exactly the
specified array size, the terminator is ignored. In this case, it matters
little, since '\0' is included in the "whatever is in quotes" bit.

In the following case:

char d[4] = "abcd";

the code is legal, d[0] is 'a', ... d[3] is 'd', and there is no null
terminator, so d is not a string.

--
Richard Heathfield : bi****@eton.pow ernet.co.uk
"Usenet is a strange place." - Dennis M Ritchie, 29 July 1999.
C FAQ: http://www.eskimo.com/~scs/C-faq/top.html
K&R answers, C books, etc: http://users.powernet.co.uk/eton
Nov 13 '05 #12
JC
Thanks for help!!

JC

"Steve Zimmerman" <st******@sonic .net> wrote in message
news:3F******** ******@sonic.ne t...
JC wrote:
hi,

i want to combine two string together.. and put in to another string. how can i do . i try myself.. with the follow code. but seem can't get the
result i want.. i want to get the result with "c is abcd" .
#include <stdio.h>
#include <string.h>
void main() {
char a[2]="ab";
char b[2]="cd";
char c[4]=" \0";
strcpy(c,a);
strcat(c,b);
printf("a is %s\n",a);
printf("b is %s\n",b);
printf("c is %s\n",c);
}

i only get this result

a is b?
b is b?
c is ab?b?

what problem to my coding? anything wrong?
please help!!.

Thanks

JC

ps. if i use strcpy(c,"ab"); and strcat(c,"cd"); i can get the result.."c is abcd"


JC,
Thank you for your question.

#include <stdio.h>
#include <string.h>

int main()
{
char a[] = "ab";
char b[] = "cd";

char c[5];

strcpy(c, a); /* Instead of these two lines, you may write */
strcat(c, b); /* strcpy(c, strcat(a, b)); */

printf("%s\n", c);

return 0;
}

--Steve

Nov 13 '05 #13
Richard Heathfield <do******@addre ss.co.uk.invali d> wrote:
Irrwahn Grausewitz wrote:
CBFalconer <cb********@yah oo.com> wrote:
JC wrote:

<SNIP>
char c[4]=" \0";

I'm not sure what this is, but it isn't a string either.

To me it looks like it is a string consisting of three blanks
followed by a null character, inititialized invoking undefined
behaviour by writing beyond array bounds.


No, the string literal comprises "whatever is in quotes" plus a null
terminating character. If "whatever is in quotes" comes to exactly the
specified array size, the terminator is ignored. In this case, it matters
little, since '\0' is included in the "whatever is in quotes" bit.


Right, of course, but I was referring to c, not the string literal it
was initialized with. I did not express myself clearly though.

<SNIP>

--
Computer: a million morons working at the speed of light.
Nov 13 '05 #14
JC
sorry . i got one more problem
i got a string with 4 char. i want to put that in a string with 26 char. how
can i fill space on the remain char.. ??
any sample method?

please help!

thanks
Jack
Nov 13 '05 #15
Irrwahn Grausewitz wrote:
Richard Heathfield <do******@addre ss.co.uk.invali d> wrote:
Irrwahn Grausewitz wrote:
CBFalconer <cb********@yah oo.com> wrote:

JC wrote:
<SNIP>
> char c[4]=" \0";

I'm not sure what this is, but it isn't a string either.

To me it looks like it is a string consisting of three blanks
followed by a null character, inititialized invoking undefined
behaviour by writing beyond array bounds.


No, the string literal comprises "whatever is in quotes" plus a null
terminating character. If "whatever is in quotes" comes to exactly the
specified array size, the terminator is ignored. In this case, it matters
little, since '\0' is included in the "whatever is in quotes" bit.


Right, of course, but I was referring to c, not the string literal it
was initialized with. I did not express myself clearly though.


Perhaps /I/ didn't express myself clearly enough. The initialisation does
/not/ invoke undefined behaviour. The end contents of c are ' ', ' ', ' ',
'\0', and the fifth character in " \0" is /not/ used for initialisation
in this special case.

--
Richard Heathfield : bi****@eton.pow ernet.co.uk
"Usenet is a strange place." - Dennis M Ritchie, 29 July 1999.
C FAQ: http://www.eskimo.com/~scs/C-faq/top.html
K&R answers, C books, etc: http://users.powernet.co.uk/eton
Nov 13 '05 #16

"JC" <JC@JC.com> wrote in message news:bl******** *@rain.i-cable.com...
sorry . i got one more problem
i got a string with 4 char. i want to put that in a string with 26 char. how can i fill space on the remain char.. ??
Fill with what? Space characters? Zero characters?

The following will let you pass the 'filler' character
as a function argument.
any sample method?


#include <stdio.h>
#include <stdlib.h>
#include <string.h>

char *copyandpad(cha r *dest, const char *src,
size_t dest_len, char fillchar)
{
size_t len = strlen(src);
strcpy(dest, src);
memset(dest + len, fillchar, dest_len - len - 1);
return dest;
}
int main()
{
char s1[] = "1234";
char s2[] = "ABCDEFGHIJKLMN OPQRSTUVWXYZ";
char fill = 'X'; /* adjust this value to your needs */

printf("Before: %s\n", s2);
copyandpad(s2, s1, sizeof s2, fill);
printf("After : %s\n", s2);
return 0;
}
Output:

Before: ABCDEFGHIJKLMNO PQRSTUVWXYZ
After : 1234XXXXXXXXXXX XXXXXXXXXXX

HTH,
-Mike
Nov 13 '05 #17
Richard Heathfield <do******@addre ss.co.uk.invali d> wrote:
Irrwahn Grausewitz wrote:
Richard Heathfield <do******@addre ss.co.uk.invali d> wrote:
Irrwahn Grausewitz wrote:

CBFalconer <cb********@yah oo.com> wrote:

>JC wrote:
<SNIP>
>> char c[4]=" \0";
>
>I'm not sure what this is, but it isn't a string either.
>
To me it looks like it is a string consisting of three blanks
followed by a null character, inititialized invoking undefined
behaviour by writing beyond array bounds.

No, the string literal comprises "whatever is in quotes" plus a null
terminatin g character. If "whatever is in quotes" comes to exactly the
specified array size, the terminator is ignored. In this case, it matters
little, since '\0' is included in the "whatever is in quotes" bit.


Right, of course, but I was referring to c, not the string literal it
was initialized with. I did not express myself clearly though.


Perhaps /I/ didn't express myself clearly enough. The initialisation does
/not/ invoke undefined behaviour. The end contents of c are ' ', ' ', ' ',
'\0', and the fifth character in " \0" is /not/ used for initialisation
in this special case.


<slaps forehead> Most certainly I didn't engage my brain before reading.
:)

Irrwahn
--
Great minds run in great circles.
Nov 13 '05 #18
"JC" <JC@JC.com> wrote in message news:<bl******* **@rain.i-cable.com>...
sorry . i got one more problem
i got a string with 4 char. i want to put that in a string with 26 char. how
can i fill space on the remain char.. ??
any sample method?


put it at the beginning of the 26 char string ?

1. use memset() to set all 26 elements of your
destination string to the space character.
2. use assignment to set the last element of
your destination array to 0 (zero) so that it
is a valid string.
3. use memcpy() to copy 4 chars from the 4 element
string to the destination string.

check your textbook to find out what memset and memcpy do.

goose,
hth
Nov 13 '05 #19
On Sat, 27 Sep 2003 16:56:55 +0800, "JC" <JC@JC.com> wrote:
sorry . i got one more problem
i got a string with 4 char. i want to put that in a string with 26 char. how
can i fill space on the remain char.. ??
any sample method?

When you say 4 char, does that include the terminating '\0' or not?

If you strcpy a string with 4 characters to an array of 26 char, the 4
(or 5) characters of the string (including the '\0') will be copied to
the first 4 (or 5) elements of the array. The remainder of the array
is untouched. If you pass this array to any string function or use it
in any string context, only the 4 (or 5) char in the string
participate. There is no need to fill in the "extraneous " portion of
the array.

If you want the resulting string to actually be 26 char long
(including the '\0') and you want to pad the right end of the string
with some char, you could do something like

char c = '?'; /*whatever pad character you like*/
size_t len;
len = strlen(array);
if (len < 25){
memset(array+le n, c, 25-len);
array[25] = '\0';
}
<<Remove the del for email>>
Nov 13 '05 #20

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

44
5826
by: Nicolas | last post by:
On most implementations of the standard C library, the functions that copy characters in a previously allocated buffer, like strcpy or strcat, are returning the pointer to that buffer. On my NetBSD system, man strcpy gives the following prototype : char *strcpy(char * restrict dst, const char * restrict src); What's the point in returning a pointer we already know before the call? Thank you in advance for an explanation.
9
12798
by: Pascal Damian | last post by:
I read somewhere that strcpy() is safer when dealing with malloc()-ed strings. Is that true? (Of course I know that both are unsafe). -- Pascal
81
7372
by: Matt | last post by:
I have 2 questions: 1. strlen returns an unsigned (size_t) quantity. Why is an unsigned value more approprate than a signed value? Why is unsighned value less appropriate? 2. Would there be any advantage in having strcat and strcpy return a pointer to the "end" of the destination string rather than returning a
14
3722
by: zeroDontSpamtype | last post by:
Hi, Why do strcpy and strcat (and strupr and strlwr in some nonstandard implementations) return a char*? Surely the logical (and DMA-safe) )return type for these would have been void?? Thanks, James McLaughlin.
24
3153
by: diego.arias.vel | last post by:
Hi all I have certain problem when I'm doing this: void copy(char *filename) { char *log_file; log_file=filename; strcat(log_file,"-log.txt");
3
16836
by: kaizen | last post by:
Hi, i wrote the code in C and compiled in VC++ compiler. at that time it has thrown the below mentioned error. error C2664: 'strcpy' : cannot convert parameter 2 from 'char' to 'const char *' Conversion from integral type to pointer type requires reinterpret_cast, C-style cast or function-style cast
13
1938
by: steve yee | last post by:
strcpy() returns it's input parameter. i asked a similar question on c++, most people seem to not think it as a problemic design. how do you think of this? http://groups.google.com/group/comp.lang.c++/browse_thread/thread/fd0aa4307b91bad8
4
9542
by: chikito.chikito | last post by:
1. Can someone tell me the difference between these two functions: void strcpy(char *s1, const char *s2) { while(*s1++ = *s2++) ; } //function prototype of strcpy follows char *strcpy(char *s1, const char *s2) // library function
0
9816
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, weíll explore What is ONU, What Is Router, ONU & Routerís main usage, and What is the difference between ONU and Router. Letís take a closer look ! Part I. Meaning of...
0
10840
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
10546
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
10588
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
10254
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
6978
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5823
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
4448
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
3
3112
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.