Word splitting error

Hello,I am using a thermal receipt printer and it has 32 char in every line, I have a code that checks for a long sentence and splits it without splitting words. I am getting an error and I was unable to fix it. Does anybody know what might cause it? Here is the code;

String sentence = F("Given the choice of anyone in the world, whom would you want as a dinner guest?");
int firstClosingBracket;
  int secondClosingBracket;
  int thirdClosingBracket;

   firstClosingBracket = sentence.indexOf(' ');
        if(firstClosingBracket < 32)
        { 
          testt = firstClosingBracket;
          secondOpeningBracket = firstClosingBracket + 1;
          secondClosingBracket = sentence.indexOf(secondOpeningBracket,' ');
          
        }
        if(secondClosingBracket < 32)
        {
          testt = secondClosingBracket;
          thirdOpeningBracket = secondClosingBracket + 1;
          thirdClosingBracket = sentence.indexOf(thirdOpeningBracket,' ');
          
        }

        count = testt ;
       Serial.write(sentence.substring(0,count) + "\n" + sentence.substring(count,64));

Hello, I am using a thermal receipt printer and it has 32 char in every line, I have a code that checks for a long sentence and splits it without splitting words. I am getting an error and I was unable to fix it. Does anybody know what might cause it? Here is the code;

String sentence = F("Given the choice of anyone in the world, whom would you want as a dinner guest?");
     int firstClosingBracket;
     int secondClosingBracket;
     int thirdClosingBracket;
     int firstOpeningBracket;
     int secondOpeningBracket;
     int thirdOpeningBracket;
     firstClosingBracket = sentence.indexOf(' ');
        if(firstClosingBracket < 32)
        {
          testt = firstClosingBracket;
          secondOpeningBracket = firstClosingBracket + 1;
          secondClosingBracket = sentence.indexOf(secondOpeningBracket,' ');
          
        }
        if(secondClosingBracket < 32)
        {
          testt = secondClosingBracket;
          thirdOpeningBracket = secondClosingBracket + 1;
          thirdClosingBracket = sentence.indexOf(thirdOpeningBracket,' ');
          
        }

        count = testt ;
       Serial.write(sentence.substring(0,count) + "\n" + sentence.substring(count,64));

The program is long, I shorted it to make it easier to see. Thanks for the reply

post a full program - as short as showing your problem.

You shortened the code so much that it can never be be compiled and also did not post the error message that you get

Please post a complete program that illustrates the problem and the full error message

I am getting an error and I was unable to fix it.

And the error would be what exactly? Or is that a secret?

Please post your whole code and the error messages exactly as they appear.

Duplicate topics merged

Cross-posting is against the rules of the forum. The reason is that duplicate posts can waste the time of the people trying to help. Someone might spend 15 minutes (or more) writing a detailed answer on this topic, without knowing that someone else already did the same in the other topic.

Repeated cross-posting will result in a suspension from the forum.

In the future, please take some time to pick the forum board that best suits the topic of your question and then only post once to that forum board. This is basic forum etiquette, as explained in the sticky "How to use this forum - please read." post you will find at the top of every forum board. It contains a lot of other useful information. Please read it.

Thanks in advance for your cooperation.

Why are you splitting them at run time when they fixed bits of text?
Instead of:

String sentence = F("Given the choice of anyone in the world, whom would you want as a dinner guest?");
String sentence0 = F("Given the choice of anyone in");
String sentence1 = F(" the world, whom would you want");
String sentence2 = F(" as a dinner guest?");

Better still don't use Strings (capital S) as they cause all sorts of problems.

const PROGMEM char sentence0[] = ("Given the choice of anyone in");
const PROGMEM char sentence1[] = (" the world, whom would you want");
const PROGMEM char sentence2[] = (" as a dinner guest?");

Has anyone introduced the String class to the F() macro?

I don’t like Arduino Strings, but this an working example.

/*
  by noiasca
  https://forum.arduino.cc/index.php?topic=667795.0
*/

String sentence;

byte printNchar(byte start, byte chars)
{
  int pos = start;
  byte oldpos = start;
  while (true)
  {
    pos = sentence.indexOf(' ', oldpos);  // search position of blank,
    if (pos >= start + chars - 1)         // found position exceeds display limit
      break;
    if (pos == -1)                        // blank not found (end of line)
      break;
    oldpos = pos + 1;                     // new search next position 
  }
  if (pos == -1) oldpos = sentence.length() - 1;
  Serial.println(sentence.substring(start, oldpos));
  return oldpos;
}

void setup() {
  Serial.begin(115200);
  Serial.println(F("\nStart"));
  //            012345678901234567890a012345678901234567890b012345678901234567890c012345678901234567890d
  //            a123456789b12345678c123456789d123456789e123456789f123456789h123456789i123456789
  sentence = F("Given the choice of anyone in the world, whom would you want as a dinner guest?");

  Serial.println(F("a123456789b12345678c123456789d1")); // just a length check in the serial monitor
  byte from = 0;

  while (from < sentence.length() - 1)
    from = printNchar(from, 32);
}

void loop() {

}

gives:

Start
a123456789b12345678c123456789d1
Given the choice of anyone in 
the world, whom would you want 
as a dinner guest
78 chars printed

Thanks for the help that really what I needed. I see that I needed to use Serial.print instead of Serial.write. Made those changes in the code and using your function and it works flawlessly @

noiasca:
I don’t like Arduino Strings, but this a working example.

/*

by noiasca
  https://forum.arduino.cc/index.php?topic=667795.0
*/

String sentence;

byte printNchar(byte start, byte chars)
{
  int pos = start;
  byte oldpos = start;
  while (true)
  {
    pos = sentence.indexOf(’ ', oldpos);  // search position of blank,
    if (pos >= start + chars - 1)        // found position exceeds display limit
      break;
    if (pos == -1)                        // blank not found (end of line)
      break;
    oldpos = pos + 1;                    // new search next position
  }
  if (pos == -1) oldpos = sentence.length() - 1;
  Serial.println(sentence.substring(start, oldpos));
  return oldpos;
}

void setup() {
  Serial.begin(115200);
  Serial.println(F("\nStart"));
  //            012345678901234567890a012345678901234567890b012345678901234567890c012345678901234567890d
  //            a123456789b12345678c123456789d123456789e123456789f123456789h123456789i123456789
  sentence = F(“Given the choice of anyone in the world, whom would you want as a dinner guest?”);

Serial.println(F(“a123456789b12345678c123456789d1”)); // just a length check in the serial monitor
  byte from = 0;

while (from < sentence.length() - 1)
    from = printNchar(from, 32);
}

void loop() {

}




gives:


Start
a123456789b12345678c123456789d1
Given the choice of anyone in
the world, whom would you want
as a dinner guest
78 chars printed

fine if it works. appreciate.