How to merge strings?

Hi,

How can I merge two strings?
Let’s say I have strings:

char* myString = “test”;
char* mySecondString = “done”;
char* testString = “testdone”;

and now I need to merge myString and mySecondString to get a string equal to testString.

I’ve tried:

  • myString,mySecondString
    and
  • strcat(myString,mySecondString)

but I do not get a string equal to testString…

any idea?

You need to declare a buffer big enough to hold the merged string.

char* myString = “test”;
char* mySecondString = “done”;
char buffer[9]; // larger enough to hold the combined string and the terminating null

strcpy(buffer, myString);
strcat(buffer, mySecondString);

mem what do I make wrong?

char* myString = "test";
char* mySecondString = "done";
char* testString = "testdone";
char buffer[9]; 

void setup()
{
  Serial.begin(9600);
  strcpy(buffer, myString);
  strcat(buffer, mySecondString);
}

void loop()
{    
  if(buffer == testString){
    Serial.println("true");
  }else{
    Serial.println("false");
  }   
  
}

I get “false”.

buffer == testString

Is not a valid comparison for C strings.

“strcmp (testString, buffer) == 0”

Explanation:
“buffer” is the address of the memory location “buffer [0]”, and “testString” is the address of “testString [0]” ; the two values are unlikely to ever be equal.

“strcmp (testString, buffer) == 0”

it works, thanks a lot!