This can do what you are asking:
char LargeTmpString[4096];//so it's large enough to do almost any text... Change it or make it dynamic if you wish. Make sure it's global.
char * TextFitToWidth(char *Txt,int Width,bool WordWrap){
int LastSpace=0;
int LinePosition=0;
int tmpCharPos=0;
for (int i=0;i<strlen(Txt);i++){
if (Txt[i]==' ') LastSpace=i;
if (Txt[i]=='\n') LinePosition=-1;
if (LinePosition==Width){//go to last space and make a new line
if (WordWrap){
tmpCharPos-=(i-LastSpace);
i=LastSpace;
LinePosition=0;
LargeTmpString[tmpCharPos]='\n';
}
else {
LargeTmpString[tmpCharPos]='\n';
tmpCharPos++;
LargeTmpString[tmpCharPos]=Txt[i];
LinePosition=1;
}
}
else{
LargeTmpString[tmpCharPos]=Txt[i];
LinePosition++;
}
tmpCharPos++;
}
LargeTmpString[tmpCharPos]='\0';
return LargeTmpString;
}
The fastest code is the code never written.