comparing strings or character arrays for radio com?

I'm trying to create my own command protocol for my two radios so that I can change parameters without having to reprogram the MCU.

I'm able to send a string of command over serial and convert it into a char array which then is being sent to the other radio. Here is the problem, I don't know if i should leave the data I received as a char array or convert it into a string to do comparison? Does working with string increases memory usage or have any negative impact compare to individual character index in an array?

Example,

I send the string command "set motor1 off", that is converted into character array. When the other radio receives it, I could convert the array into a string and do sub string to find key words that will trigger a parameter change. Or I could leave it as character array and compare individual elements which then triggers a parameter change.

Here are code snippets examples not really what my code is.

 //( These snippets may not work but hopefully you get the point)

for (int i = 0; i < size of(serialCharReceived); i++) {

// get char array into a string

  String rxString += serialCharReceived[i]

} 
if (rxString == "set motor1 off") {
  // shut off motor 1

}

OR

if (rxCharArray[0] == 's' && rxCharArray[1] == 'e' && rxCharArray[2] == 't' ) {
  if (rxCharArray[4] == 'm' && rxCharArray[5] == 'o' && rxCharArray[6] == 't' && rxCharArray[7] == 'o' && rxCharArray[8] == 'r' && rxCharArray[9] == '1') {
    if (rxCharArray[11] == 'o' && rxCharArray[12] == 'f' && rxCharArray[13] == 'f' &&){

      // turn off motor 1
    }
  }

}

Don't use String (object), use string (char array with null terminator).

Declare rxString string in global scope with a fixed length as showed in the code below. (You determine the SIZE_OF_rxString value).

To compare strings use strcmp() (google it).
To copy strings use strcpy() (google it).

Edit: Corrected the code.
The equivalent code:

char rxString[SIZE_OF_rxString];

strcpy(rxString,serialCharReceived);

if ( !strcmp(rxString,"set motor1 off") ) {
  // shut off motor 1
}

 !strcmp(&rxCharArray[0],"set"){
  !strcmp(&rxCharArray[4],"motor1"){
    !strcmp(&rxCharArray[11],"off"){
        
       // turn off motor 1
    }
  }
}

Thanks for the reply giova014.

why do you use the & when comparing the string command? What does it mean?

 !strcmp(&rxCharArray[0],"set")

I added the missing parenthesis and if at this part of the code:

 if(!strcmp(&rxCharArray[0],"set")){
  if(!strcmp(&rxCharArray[4],"motor1")){
    if(!strcmp(&rxCharArray[11],"off")){
       
       // turn off motor 1
    }
  }
}

The & before a variable -> Returns the address of the variable.

&rxCharArray[7] -> Will return the address of char 7 of the array rxCharArray.

The first argument of strcmp() takes a pointer, so it will get a pointer beginning at char 7.

You want to compare three comands:
The first begins at char 0,
the second begins at char4,
and the third begins at char11.

Just a note: at char 0, I could use just rxCharArray (because this already points at char 0), but I did write &rxCharArray[0] just to padronize the code.

If your string is: "set/motor/off" and you need to compare to "motor", you can't start by the char 0, so start at char 4!

Thank you, everything is working now!

Now i'm wonder why you have "!" before strcmp. The strcmp returns 0 for the same, or positive/negative depending if string 1 is greater/less than string 2.

If the if-statement is not true meaning if the strcmp is not 0 proceed within the block?

The if statement only checks if the value is TRUE or FALSE.

It evaluates to FALSE if the value is 0.
And to TRUE if the value is anything different from 0.

In the strcmp(), the value is 0 if the string are equal, so it would be FALSE, but adding the ' ! ', it will be TRUE if equal.

Rather than this:

if(!strcmp(&rxCharArray[0],"set")){

You could use:

if(strcmp(&rxCharArray[0],"set") == 0){