Jump to content

C++ Wordwrapping


Recommended Posts

What you could do is store all the information to an apstring or something and then use the string.length() craption to get the length. Then only print a lines worth and then go to the next line with a for loop. Input the whole sentence into the string using the getline function or else this will just flop.

Share this post


Link to post
Share on other sites

Moo, how's about this one...

 

void printww(char *text) {
    int conWidth = 80;
    int i = 0, j = 0;
    int strLen = 0;
    int sub = 0;
    char *cpyText, *start, *tmp, *searchPoint;
    
    strLen = strlen(text);         
    if (strLen > conWidth) {
       cpyText = (char *)malloc(strLen * sizeof(char));
       assert(cpyText != NULL);
       memcpy(cpyText, text, strLen);
       cpyText[strLen] = '\0';
       tmp = cpyText;   
                          
       for (i = conWidth; i < strLen; i += conWidth) {           
           if (cpyText[i - 1] != ' ') {
              searchPoint = &cpyText[i - 1];
             
              // find previous space
              sub = 0;
              start = NULL;
              for (j = 0; j <= i; j++) {
                  if (*(searchPoint - j) == ' ') {
                     start = (searchPoint - j);
                     break;
                  }                   

                  sub++;
              }     
              
              // start is too long, set start to searchPoint to continue
              if (start == NULL) {
                 start = searchPoint;          
              } else {              
                 // speshulness;-)     
                 i -= sub;                       
                             
                 // stick the whole word on new line
                 *start = '\n';     
              }                 
              
              // search again from the current word (just after the new \n)                               
              tmp = (start + 1);             
           }
       }

       printf("%s", cpyText);        
    } else {
       printf("%s", text);                 
    }
}

 

I'm going to sleep now :sleep:

Share this post


Link to post
Share on other sites

Yeah humm markie's look's a little more complecated than the Java version i coded back in Feb. or March. Can't remember when, but eitherway it looks like it gets the job done....It's amazing how close so many languages are to each other, I can read diff coding languages, yet

true == ((me + Spanish) != good)

Yeah well looks like markie saved the day so cya later.

Share this post


Link to post
Share on other sites

Please sign in to comment

You will be able to leave a comment after signing in



Sign In Now
 Share

×
×
  • Create New...