Function to prepend a new string to an existing string

The Prepend() function can be used to insert a new string at the front of an existing string. The programmer must insure that the existing string has been allocated enough memory for both strings combined plus the null termination character. The function does not chew up any additional memory for intermediate results. I haven’t beat on this too much, so there may be hiccups remaining, but it’s a starting point.

void setup() {

  This function inserts a string in front of an existing string. See caution below.

Parameter list:
  char dest[]        The string to receive the insert
  char insert[]      The string to prepend to the dest[] string

Return value:

CAUTION: It is up to the programmer to insure that the dest[] string
         has enough space for both strings plus the null. Failure to plan
         for that amount of memory can cause nasty things to happen.
void Prepend(char dest[], char insert[])
  int i;
  int lengthDest = strlen(dest);
  int lengthInsert = strlen(insert);

  for (i = lengthDest - 1; i > -1; i--) {
    dest[i + lengthInsert] = dest[i];
  for (i = 0; i < lengthInsert; i++) {
    dest[i] = insert[i];
  dest[lengthDest + lengthInsert] =  '\0';

void loop() {                                    // Code to test it
  char arg1[20];
  char arg2[50];
  int charsRead;

  Serial.println("Enter string to be inserted: ");
  while (true) {
    if (Serial.available() > 0) {
      charsRead = Serial.readBytesUntil('\n', arg1, 19);
      arg1[charsRead] = '\0';
  Serial.println("Enter string to receive the insert: ");
  while (true) {
    if (Serial.available() > 0) {
      charsRead = Serial.readBytesUntil('\n', arg2, 29);
      arg2[charsRead] = '\0';
  Prepend(arg2, arg1);