CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 9 of 9
  1. #1
    Join Date
    May 1999
    Posts
    12

    Copy 2 Strings in a third string

    hi folks

    i have problem

    i want to copy String_A and String_B in String_C

    how can i make this????

    after copy the String_C should look like STRING_ASTRING_B

    the source i've tried looks like....


    int SadClass::Add_To_SadString()
    {
    char * StringA=NULL;

    if(StringC!=NULL)
    {
    StringA= new char[StringSize*CountRecord];

    strcpy(StringA,StringC);

    delete StringC;
    }

    StringC= new char[(int)StreamSize];

    if(StringA!=NULL)
    {
    strcpy(StringC,StringA);

    delete StringA;
    }

    strncpy(StringC + CountRecord * StringSize,StringB ,(int)StreamSize);


    CountRecord++;

    return 0;
    }





    please HELP.....

    thanks


  2. #2
    Join Date
    May 1999
    Location
    Wisconsin, USA
    Posts
    953

    Re: Copy 2 Strings in a third string

    Are the strings defined as CStrings or arrays of characters?

    If they are CStrings you should be able to do something like this:

    String_C = String_A + String_B;

    or

    String_C = String_A + " " + String_B;

    If they are arrays of characters try something like this:

    strcpy(String_C, String_A)
    strcat(String_C, String_B);

    Hope this helps.



  3. #3
    Join Date
    Apr 1999
    Posts
    12

    Re: Copy 2 Strings in a third string

    Use strcat


  4. #4
    Guest

    Re: Copy 2 Strings in a third string

    Really it depends on what kind of strings you are using. If you are using CStrings simply use the + or += operator

    CString a = "Hello";
    CString b = "World";
    CString c;
    CString d = "Momma";

    c = a+b; // sets c to "Hello World"
    a += d; // set a to "Hello Momma"

    If you are using generic character pointers you need to use the strcat function.

    char* a = "Hello";
    char* b = "World";
    char* c;
    strcpy (c,a); //sets c to "Hello"
    strcat (c,b); // sets c to "Hello Word"

    Finally if you are using character arrays you can do the same thing by extending the size of your array, then copying the characters from the other array over.

    Make sure that you are careful not to overwrite memory as this would cause problems.

    Rob P.


  5. #5
    Join Date
    May 1999
    Posts
    12

    Re: Copy 2 Strings in a third string

    thanks allot

    the command strcat was new for me.

    now it works.


  6. #6
    Join Date
    May 1999
    Posts
    12

    Re: Copy 2 Strings in a third string

    thank you

    the command strcat was new for me.

    now it works.


  7. #7
    Join Date
    May 1999
    Posts
    12

    Re: Copy 2 Strings in a third string

    thank you peter

    it was very helpfull

    the command strcat was new for me.

    thanks


  8. #8
    Join Date
    May 1999
    Location
    Sydney, Australia
    Posts
    420

    Re: Copy 2 Strings in a third string

    char* a = "Hello";
    char* b = "World";
    char* c;
    strcpy (c,a); //sets c to "Hello"
    strcat (c,b); // sets c to "Hello Word"

    doesn't work that well, as char* c doesn't have memory allocated to it

    sally


Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured