Learn "char type" c-strings first (not String with capital-S) and then practice "String type" strings.
Example-1: "char type" c-string
Send the string "Hello" from the InputBox of Serial Monitor (Fig-1) with "Line ending tab" at "Newline" option. Receive the string by UNO and then send it to OutputBox of Serial Monitor.
char myData[10] = "";
void setup()
{
Serial.begin(9600);
}
void loop()
{
byte y = Serial.available(); //check that a char has come from Serial Monitor
if(y != 0)
{
byte m = Serial.readBytesUntil('\n', myData, 10); //read and save until Newline
myData[m] = '\0'; //insert null-byte as last element in char-type array
Serial.print(myData); //show received message on Serial Monitor
Serial.println(); //insert line feed
//-----------------------------------
memset(myData, 0, 10); //reset the array
}
}
2. Open the Serial Monitor at 9600 bd. 3. Choose Newline option for the Line ending tab. 4. Enter Hello in the InoutBox and then click on Send Button. 5. Check that Hello has appeared on the OutputBox (Fig-2). 6. Enter Welcome in the InoutBox and click on the Send button. 7. Check that Welcome has appeared on the next line of Hello (Fig-2).
TheMemberFormerlyKnownAsAWOL:
No, I mean "consider the case where 'm' equals 10".
But "oops" is shorter, and is meant to convey the meaning "there's something wrong here, maybe you should reconsider your code"
I hope that I have got your point.
When m = 10, it means 10 characters have already been stored in the array. Now, there is no more space in the array to hold the null-character as the array size is only 10. So, array size should be at least 11. My sketch worked; because, the entered string had less than 10 charcaters.
I have no words to repay for your meticulous observation.