Format string function
-
Hello, I want a function which will format some text with carriage returns in it. So that I have a maximum width like 80 characters. If my string is perhaps 180 characters I would like to look through the string and every 80 characters replace the nearest space character to the left of my maximum width with a carriage return. Is there a function that anyone has that can do this? Thank you :)
-
Hello, I want a function which will format some text with carriage returns in it. So that I have a maximum width like 80 characters. If my string is perhaps 180 characters I would like to look through the string and every 80 characters replace the nearest space character to the left of my maximum width with a carriage return. Is there a function that anyone has that can do this? Thank you :)
the screen driver does this automatically. anyway, you could do the simple (according that your string is str, C-style string):
int iCpt = 0;
while (str[iCpt] != '\0') {
if (!(iCpt%80)) {
printf("\n");
}
printf("%c", str[iCpt]);
iCpt++;
}or the more complex code below (i'm not even sure it works) :
int iCpt = 0, iResult = 80;
while (iResult == 80) {
iResult = printf("%80s\n", str[iCpt]);
iCpt += iResult;
}
TOXCCT >>> GEII power
-
the screen driver does this automatically. anyway, you could do the simple (according that your string is str, C-style string):
int iCpt = 0;
while (str[iCpt] != '\0') {
if (!(iCpt%80)) {
printf("\n");
}
printf("%c", str[iCpt]);
iCpt++;
}or the more complex code below (i'm not even sure it works) :
int iCpt = 0, iResult = 80;
while (iResult == 80) {
iResult = printf("%80s\n", str[iCpt]);
iCpt += iResult;
}
TOXCCT >>> GEII power
I do not want to break up any whole words. I can show you. If my maximum width is 8 characters and this is my text: aaa bbbb ccccc ddddd eeeeeee fff gg hhhhh I want to have this: aaa bbbb\nccccc\nddddd\neeeeeee\nfff gg\nhhhhh So my result would be this: aaa bbbb ccccc ddddd eeeeeee fff gg hhhhh
-
I do not want to break up any whole words. I can show you. If my maximum width is 8 characters and this is my text: aaa bbbb ccccc ddddd eeeeeee fff gg hhhhh I want to have this: aaa bbbb\nccccc\nddddd\neeeeeee\nfff gg\nhhhhh So my result would be this: aaa bbbb ccccc ddddd eeeeeee fff gg hhhhh
ahh ok, well if you don't say it, how should i guess... hem, i'm thinking of this : * add a marker to record the last whitespace you encountered before the end of the line, * create a buffer 81 caracters long (because of the trailing '\0') where you copy the caracters (and so WORDS) to printf, * when you encountered the end of a line, or the end of the source string, copy the buffer to the screen
TOXCCT >>> GEII power
-
Hello, I want a function which will format some text with carriage returns in it. So that I have a maximum width like 80 characters. If my string is perhaps 180 characters I would like to look through the string and every 80 characters replace the nearest space character to the left of my maximum width with a carriage return. Is there a function that anyone has that can do this? Thank you :)
suppose your string is char str[]="......"
#define MIN(a, b) (((a) < (b)) ? (a) : (b)) int str_len = strlen(str); int LINE_LEN=7; //you will nead here 80 int c=(int)ceil((float)str_len/(float)LINE_LEN); char *str2= new char[str_len+c+1]; int n=0; int m=0; for(int k=0;k Pain is a weakness living the body