void setup() {
  // put your setup code here, to run once:
  Serial.begin(115200);
  Serial.println("Start");


 String inputString = "1234567890\n1234567890 Franco ceccarellacicciocbabcdefgh armando imma gianna\n anna pino peppe luca andrea giorgio";
 //  String inputString = "12:34:56\n12/12/12";
  int maxLength = 20;
  int maxLines = 8;
  splitString(inputString, maxLength, maxLines);

  return;



}

void loop() {
  // put your main code here, to run repeatedly:
  delay(10); // this speeds up the simulation
  //printf("kjhkjhk");
}


void splitString(const String &input, int maxLength, int maxLines) {
  int length = input.length();
  int start = 0;
  bool large = false;
  int nb_lines = 0;
  while (start < length) {
    //Serial.printf("start %d\n", start);
    // remove leading spaces
    while (input[start]== ' ')
      start++;
    int end = start + maxLength;
    if (end > length) {
      end = length;
    }

    // Find the last space within the current segment
    while (end >=0 && end < length && input[end] != ' ' && input[end] != '\n') {
      end--;
      //Serial.printf("end %d\n", end);
    }
    // If no space or \n found, break the word
    large = false;
    if (end < start) {
      printf("qui");
      end = start + maxLength;
      large = true;
    }
    else {
    // Find the first \n within the current segment
      for (int k = start;  k < end; k++ ) {
        if (input[k] == '\n') {
          end = k;
          break;
        }
      }

    }
    String s = input.substring(start, end);
    //s.trim();
    //s.replace("\n", "\\n");
    Serial.printf("%s\n", s.c_str());

    if (++nb_lines >= maxLines)
      break;

    // Update the starting index for the next segment
    if (large)
      start = end;
    else
      start = end + 1;
  }
}
esp:0
esp:2
esp:4
esp:5
esp:12
esp:13
esp:14
esp:15
esp:16
esp:17
esp:18
esp:19
esp:21
esp:22
esp:23
esp:25
esp:26
esp:27
esp:32
esp:33
esp:34
esp:35
esp:3V3
esp:EN
esp:VP
esp:VN
esp:GND.1
esp:D2
esp:D3
esp:CMD
esp:5V
esp:GND.2
esp:TX
esp:RX
esp:GND.3
esp:D1
esp:D0
esp:CLK