cshort@haywire.crl (Spmg*d, Lord of Potted Meat Product) (06/05/91)
hi could someone show me some example code to take two char strings and combine them into another char string. thanks chris -- |----------------------|---------------------- | |the world is spam and | Chris Short | | we are but the key | Computing Research Labatory | |--------|-------------| Box 30001/3 CRL | | New Mexico State University | | Las Cruces, NM 88003-0001 | | Email: Cshort@nmsu.edu Fax:505 646 6218|-------------------| | Voice: 505 646 6216 |SpamKey productions| | -------------------------|-------------------|
gordon@osiris.cso.uiuc.edu (John Gordon) (06/05/91)
cshort@haywire.crl (Spmg*d, Lord of Potted Meat Product) writes: >hi >could someone show me some example code to take >two char strings and combine them into another char >string. char *str1 = "Joseph went"; char *str2 ' " to the store."; char str3[100]; strcat(str3, str1); /* tacks str1 onto the end of str3 */ strcat(str3, str2); /* tacks str2 onto the end of str3 */ /* str3 should now be "Joseph went to the store." */ --- John Gordon Internet: gordon@osiris.cso.uiuc.edu #include <disclaimer.h> gordon@cerl.cecer.army.mil #include <clever_saying.h>
kherron@ms.uky.edu (Kenneth Herron) (06/05/91)
cshort@haywire.crl (Spmg*d, Lord of Potted Meat Product) writes: >hi >could someone show me some example code to take >two char strings and combine them into another char >string. ---------- int main() { #include <stdio.h> char *a = "first string", *b = "second string", c[128], *d = c; while (*a && *b) *d++ = *a++ + *b++; *d = '\0'; puts(c); exit(0); } --------- How exactly did you want these strings combined? -- Kenneth Herron kherron@ms.uky.edu University of Kentucky +1 606 257 2975 Department of Mathematics "So this won't be a total loss, can you make it so guys get to throw their mothers-in-law in?" "Sure, why not?"
scs@adam.mit.edu (Steve Summit) (06/05/91)
In article <1991Jun4.210209.28463@ux1.cso.uiuc.edu> gordon@osiris.cso.uiuc.edu (John Gordon) writes: >cshort@haywire.crl (Spmg*d, Lord of Potted Meat Product) writes: >>could someone show me some example code to take >>two char strings and combine them into another char >>string. > >char *str1 = "Joseph went"; >char *str2 ' " to the store."; >char str3[100]; >strcat(str3, str1); /* tacks str1 onto the end of str3 */ >strcat(str3, str2); /* tacks str2 onto the end of str3 */ This may fail if str3 is a local variable, because it is not necessarily initialized with \0's. A safer technique would be (void)strcpy(str3, str1); (void)strcat(str3, str2); To the original requester: see also the comp.lang.c frequently- asked questions list, under the question "I can't get strcat to work." (The FAQ list also discusses static vs. automatic default initialization, although it does not explicitly mention \0 for char arrays.) Steve Summit scs@adam.mit.edu
volpe@camelback.crd.ge.com (Christopher R Volpe) (06/05/91)
In article <1991Jun4.210209.28463@ux1.cso.uiuc.edu>, gordon@osiris.cso.uiuc.edu (John Gordon) writes: |>char *str1 = "Joseph went"; |>char *str2 ' " to the store."; |>char str3[100]; |> |>strcat(str3, str1); /* tacks str1 onto the end of str3 */ And where's the end of str3 if it hasn't been initialized? Use strcpy for the first component instead. |>strcat(str3, str2); /* tacks str2 onto the end of str3 */ |> |>/* str3 should now be "Joseph went to the store." */ |> |> |>--- |>John Gordon |>Internet: gordon@osiris.cso.uiuc.edu #include <disclaimer.h> |> gordon@cerl.cecer.army.mil #include <clever_saying.h> ================== Chris Volpe G.E. Corporate R&D volpecr@crd.ge.com
glenn@zeus.ocs.com (Glenn Ford) (06/05/91)
In article <CSHORT.91Jun4131435@haywire.crl> cshort@haywire.crl (Spmg*d, Lord of Potted Meat Product) writes: >could someone show me some example code to take >two char strings and combine them into another char >string. > I think chris, you should show some spine and open a book up. It is why we all learned to read. eh? Glenn Ford glenn@zeus.ocs.com
glenn@zeus.ocs.com (Glenn Ford) (06/05/91)
In article <1991Jun4.213935.5508@ms.uky.edu> kherron@ms.uky.edu (Kenneth Herron) writes: > > while (*a && *b) > *d++ = *a++ + *b++; > *d = '\0'; This is not a strcat. Frankly, i don't know what your trying to do.. >How exactly did you want these strings combined? I think he wanted to concat string b onto string a..
gordon@osiris.cso.uiuc.edu (John Gordon) (06/05/91)
>>could someone show me some example code to take >>two char strings and combine them into another char >>string. >char *str1 = "Joseph went"; >char *str2 ' " to the store."; >char str3[100]; >strcat(str3, str1); /* tacks str1 onto the end of str3 */ >strcat(str3, str2); /* tacks str2 onto the end of str3 */ >/* str3 should now be "Joseph went to the store." */ OK, OK, so I made a mistake. The first strcat() should be a strcpy(). John
darwinl@alliance.uucp (Darwin Ling) (06/05/91)
In article <1991Jun4.210209.28463@ux1.cso.uiuc.edu> gordon@osiris.cso.uiuc.edu (John Gordon) writes: >cshort@haywire.crl (Spmg*d, Lord of Potted Meat Product) writes: > > >>hi > >>could someone show me some example code to take >>two char strings and combine them into another char >>string. > >char *str1 = "Joseph went"; >char *str2 ' " to the store."; >char str3[100]; > >strcat(str3, str1); /* tacks str1 onto the end of str3 */ >strcat(str3, str2); /* tacks str2 onto the end of str3 */ > >/* str3 should now be "Joseph went to the store." */ > > >--- >John Gordon >Internet: gordon@osiris.cso.uiuc.edu #include <disclaimer.h> > gordon@cerl.cecer.army.mil #include <clever_saying.h> Or even easier do sprintf (str3, "%s%s", str1, str2);
darcy@druid.uucp (D'Arcy J.M. Cain) (06/09/91)
In article <676362409.60@egsgate.FidoNet.Org> Glenn.Ford@f98.n250.z1.FidoNet.Org (Glenn Ford) writes:
Well we all saw what he wrote twice now. Is someone talking to egsgate
to stop the flood of dups? Hey Henry, if you want to stop duplicate
articles just treat "FidoNet" in the message ID as an error. :-)
followup to news.admin (if necessary)
--
D'Arcy J.M. Cain (darcy@druid) |
D'Arcy Cain Consulting | There's no government
Toronto, Ontario, Canada | like no government!
+1 416 424 2871 |