After doing a bit of study, I realised that the code might not be working because im incrementing text, ie. text++, when text is declared to be a const pointer. Is this allowed? If not how can I correct my code? (I have to keep the text pointer paramter const.) I tried creating a new non-const pointer to point at the start of text so I could use that, but got an invalid conversion error (which also happened when I first made the wordEnd pointer, before making that const.).
EDIT: I just found out that the way the text parameter is declared:
means that only what is pointed to is const rather than the pointer itself, right?Code:const char *text
..so I'm back to where I was..
Last edited by space-oddity; 05-01-2008 at 01:57 AM.
I looked at your code - it's quite late, and frankly it didn't hit me as the way to quite do it. I know this job can be done elegantly with pointers, but it can also be done OK without pointers.
This is a sample of the latter:
Code:#include <stdio.h> #include <string.h> void printText(void); int main(void) { printf("\n\n\n\n"); printText(); return 0; } void printText() { int i, j, left, lwidth, right, width, gar; char t[] = "The quick red fox jumped quickly and nimbly over the sleeping dog.\0"; i = left = right = lwidth = 0; width = 20; while(t[i]) { //loop the whole phrase i++; right++; if(t[right] == ' ' || t[right] == '\0') { //print the word if it if(lwidth + (right - left) <= width) { //will fit on the line for(j = left; j < right; j++) putchar(t[j]); lwidth = lwidth + (right - left); //set up for next word left = right; } else { //word wouldn't fit putchar('\n'); //on the line lwidth = 0; //start a new line for(j = left + 1; j < right; j++) { putchar(t[j]); lwidth++; } left = right; //set up for next word } //else } //if(t[i]... } //while t[i] gar = getchar(); gar++; } //gar is just shorthand for garbage value to hold the window open.
I'm thinking there's an even easier way:
Check whether strlen() returns a value greater than the allowed width, if not, print the line.
Otherwise, go to the position at the max width in the string. If it's a space, print the first part (you could use snprintf into a string and then print that, rather than printing each char individually) and then print the second part (same way as the first).
If you landed on a char, then go backwards (decrement a pointer) until you hit a space and then repeat the above.
For a nice, recursive touch, instead of just printing the second line, call your printText function on it. That will ensure that if you're printing text that's bigger than two line widths, it gets broken down properly.
QuantumPete
P.S. Please ask if you want me to elaborate on anything.
"No-one else has reported this problem, you're either crazy or a liar" - Dogbert Technical Support
"Have you tried turning it off and on again?" - The IT Crowd
Thanks for your help guys, I played around with your code adak and realised I was getting the bad output due to using the isspace function.. why was that the cause? The description of it in the man makes it look perfect for the purpose.
QuatumPete, your recursion idea looks cool and I might try it out sometime but doing it that way would mean any extra spaces in between the words would still be there, which is one of the things i was trying to eliminate, ie two spaces between two words reduces to one in output.
Last edited by space-oddity; 05-02-2008 at 07:37 PM.