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
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.
Re: Copy 2 Strings in a third string
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.
Re: Copy 2 Strings in a third string
thanks allot
the command strcat was new for me.
now it works.
Re: Copy 2 Strings in a third string
thank you
the command strcat was new for me.
now it works.
Re: Copy 2 Strings in a third string
thank you peter
it was very helpfull
the command strcat was new for me.
thanks
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