Strings on an ESP8266

I am new to C++ and the following is a routine I wrote to format a command defined as string MDCommand:

</>
void formatMDCommand(char * inputstring) {

char * Command = "";
char * field1 = "";
char * field2 = "";
char * field3 = "";
char * field4 = "";
char * field5 = "";
char * field6 = "";
char * field7 = "";
char * field8 = "";
char * field9 = "";
char * field10 = "";

#if debug
Serial.println("Formatting MD Command: ");
Serial.println("inputstring: [" + String(inputstring));
Serial.println("Delimiters: [" + String(commandDelimeters));
#endif

Command = strtok(inputstring,commandDelimeters); // get the command
MDCommand = "MD";

field1 = strtok(NULL, commandDelimeters); // get the parameter if any
field2 = strtok(NULL, commandDelimeters); // get the parameter if any
field3 = strtok(NULL, commandDelimeters); // get the parameter if any
field4 = strtok(NULL, commandDelimeters); // get the parameter if any

#if debug
Serial.println("Formatting MD Command: ");
Serial.println("field1: [" + String(field1) + "]");
Serial.println("field2: [" + String(field2) + "]");
Serial.println("field3: [" + String(field3) + "]");
Serial.println("field4: [" + String(field4) + "]");
#endif

if (field1!= "") {
MDCommand = "MD" + char(9) + String(field1);
Serial.println(MDCommand);
}
if (field2 != "") {
MDCommand = "MD" + char(9) + String(field1) + char(9) + String(field2);
Serial.println(MDCommand);
}
if (field3 != "") {
MDCommand = "MD" + char(9) + String(field1) + char(9) + String(field2) + char(9) + String(field3);
Serial.println(MDCommand);
}
if (field4 != "") {
MDCommand = "MD" + char(9) + String(field1) + char(9) + String(field2) + char(9) + String(field3) + char(9) + String(field4);
Serial.println(MDCommand);
}
#if debug
Serial.println("MD Command: [" + MDCommand + "]");
#endif
}
</>

This is what I am getting on the Serial Monitor for debugging:

Formatting MD Command:
inputstring: [795631813456 MEXICO 5/25/2019 A10012345 $⸮?⸮$⸮?
Delimiters: [|
Formatting MD Command:
field1: [MEXICO]
field2: [5/25/2019]
field3: [A10012345]
field4: [$⸮?⸮$⸮?]
1: [MEXICO
1: [MEXICO 5/25/2019
1: [MEXICO 5/25/2019 A10012345
1: [MEXICO 5/25/2019 A10012345 $⸮?⸮$⸮?
MD Command: [1: [MEXICO 5/25/2019 A10012345 $⸮?⸮$⸮?]

I don’t get why it is not formatting the “MD” at the beginning of the command. It should have been “MD{tab}MEXICO{tab}05/25/2019{tab}A10012345

It's like something is stepping on variable memory. When I compile I have the following memory left

Archiving built core (caching) in: C:\Users\Randy\AppData\Local\Temp\arduino_cache_251239\core\core_esp8266_esp8266_nodemcuv2_CpuFrequency_80,FlashSize_4M1M,LwIPVariant_v2mss536,Debug_Serial,DebugLevel_None____,FlashErase_none,UploadSpeed_115200_0b29be32429285d6ae765e2f856d5e55.a
Sketch uses 296856 bytes (28%) of program storage space. Maximum is 1044464 bytes.
Global variables use 49092 bytes (59%) of dynamic memory, leaving 32828 bytes for local variables. Maximum is 81920 bytes.

So looks like I have plenty of space.

make sure to format your code correctly using the </> symbol at the top so your code is easier to read

Maybe your inputString has not been null terminated.

I went and appended '\0' to the end of inputstring and now I get this on Serial Monitor. It did clean up the end of the input string.

Do I need to add '\0' to the strtok command when extracting the individual fields? or does strtok take care of that?

Formatting MD Command:
inputstring: [795631813456 MEXICO 5/25/2019 A10012345
Delimiters: [|
Formatting MD Command:
field1: [MEXICO]
field2: [5/25/2019]
field3: [A10012345]
]
field5: []
field6: []
field7: []
field8: []
field9: []
field10: []
1: [MEXICO
1: [MEXICO 5/25/2019
1: [MEXICO 5/25/2019 A10012345

]

Everything cstring needs to be null terminated of you want to use cstring functions on them.

For strtok(), the first argument is a cstring but the second one needs not to be.

Ok, sorry new to C++ so my command is char * and the sub fields 1 thru 10 are char * also.

Are you saying the command should be string? Not sure what you mean by cstring.

cstring just means that the string is (or needs to be) null terminated.

So the yes, the "command" should be cstring to be used as first argument of strtok ()

I need to clarify that "string" in the above post refers to array of chatacters.

Anyone know why this does not work?

if (field1!= "") {
MDCommand = "MD" + '\t' + field1;
Serial.println(MDCommand);
}

What I get on the serial monitor is:

Formatting MD Command:
inputstring: [795631813456 MEXICO 5/25/2019 A10012345
Delimiters: [|
Formatting MD Command:
field1: [MEXICO]
field2: [5/25/2019]
field3: [A10012345]

Following is the result of the Serial.println(MDCommand);

1: [MEXICO

Why does not display MD MEXICO?

field1 is defined as string field1;