UnderwareDESIGN

PlayBASIC => Resources => Source Codes => Topic started by: LemonWizard on March 14, 2009, 02:54:10 AM

Title: A smart word wrapper.
Post by: LemonWizard on March 14, 2009, 02:54:10 AM
A smart word wrapper. This took me alot longer than I thought it would...
Not used to counting strings... or counting them invisibly. (I like pre defined values.)
So... it dynamically wraps words... or anything. it shouldn't bug... but if you put in a word that's 70 characters it might crash... o_0

Enjoy.

[pbcode]
; PROJECT : Word Wrapper
; AUTHOR  : LemonWizard
; CREATED : 3/13/2009
; EDITED  : 3/13/2009
; ---------------------------------------------------------------------

;edit this string however you like, I'm certain that it won't bug. ^^ make it 350 characters long for all it cares. =P
s$="happy happy happy happy happy happy happy test test test were going for 70 here a perfect word wrapper... it's very crucial to making things appear  correctly you know so far nothings been cut off were going to use short short i i i i testsing the i u u "
cy=1
   for temp=1 to len(s$)
         if mid$(s$, temp, 1)=" "
      dtemp=1
         for temp2=1 to len(s$)-temp
         if mid$(s$, temp2+temp, 1)=" "
            exitfor
         else
            inc dtemp
         next temp2   
         
            
      if dtemp >70-cx
         cx=1
         inc cy
         dtemp=1
         endif
      endif
      endif
      
      
      setcursor cx*10, cy*10
      print mid$(s$, temp, 1)
      
      

inc cx
next temp

waitkey




[/pbcode]

Title: Re: A smart word wrapper.
Post by: Big C. on March 14, 2009, 03:04:57 AM
Hey lemon,

Quotebut if you put in a word that's 70 characters it might crash

what are meaning... do you have some example?

here it works...

But if I look deeper in your code there is a NONE NONE :)

[pbcode]
cy=1
   for temp=1 to len(s$)
         if mid$(s$, temp, 1)=" "
      dtemp=1
         for temp2=1 to len(s$)-temp
         if mid$(s$, temp2+temp, 1)=" "
            exitfor
         else
            inc dtemp
                                                                              <---- ONE STOP!! WHERE IS TEH CLOSING IF-STATEMENT "ENDIF"

         next temp2                                        
         
            
      if dtemp >70-cx
         cx=1
         inc cy
         dtemp=1
         endif
      endif
      endif
      
      
      setcursor cx*10, cy*10
      print mid$(s$, temp, 1)
      
      

inc cx
next temp

waitkey
[/pbcode]

so, here is the well structured code for you... I hope you see the different... ;)

[pbCode]
s$="happy happy happy happy happy happy happy test test test were going for 70 here a perfect word wrapper... it's very crucial to making things appear  correctly you know so far nothings been cut off were going to use short short i i i i testsing the i u u "
cy=1
For temp=1 To Len(s$)
   If Mid$(s$, temp, 1)=" "
      dtemp=1
      For temp2=1 To Len(s$)-temp
         If Mid$(s$, temp2+temp, 1)=" "
            ExitFor
         Else
            Inc dtemp
         EndIf
      Next temp2   
      
      If dtemp >70-cx
         cx=1
         Inc cy
         dtemp=1
      EndIf
   EndIf
   
   SetCursor cx*10, cy*10
   Print Mid$(s$, temp, 1)
   Inc cx

Next temp

WaitKey
[/pbcode]


Title: Re: A smart word wrapper.
Post by: geecee on March 14, 2009, 05:26:29 PM
Hi LemonWizard

Quotebut if you put in a word that's 70 characters it might crash...

No problems with a 70 character word but the first word of the message doesn't get indented.  O.K otherwise.


[pbcode];edit this string however you like, I'm certain that it won't bug. ^^ make it 350 characters long for all it cares. =P
s$="Wishing you a very happyhappyhappyhappyhappyhappyhappyhappyhappyhappyhappyhappyhappyhappy New Year"
cy=1
 for temp=1 to len(s$)
   if mid$(s$, temp, 1)=" "
      dtemp=1
      for temp2=1 to len(s$)-temp
        if mid$(s$, temp2+temp, 1)=" "
            exitfor
          else
            inc dtemp
      next temp2   
            
      if dtemp >70-cx
        cx=1
        inc cy
        dtemp=1
      endif
   endif
 endif
      
 setcursor cx*10, cy*10
 print mid$(s$, temp, 1)

 inc cx
next temp

waitkey[/pbcode]


:)
geecee

Title: Re: A smart word wrapper.
Post by: LemonWizard on March 14, 2009, 10:29:48 PM
hm yeah interesting.. I didn't notice that the first word isn't indented.
I wonder how I'd make it line the first word up with the next line below so you get a proper margine...

any ideas?
Title: Re: A smart word wrapper.
Post by: geecee on March 16, 2009, 09:52:00 PM
Hi LemonWizard

What I did was to start the message with two spaces.

s$="  Wishing you a very happyhappyhappyhappyhappyhappyhappyhappyhappyhappyhappyhappyhappyhappy New Year"

That's all I could think of.

:)
Cheers
geecee

Title: Re: A smart word wrapper.
Post by: kevin on March 17, 2009, 09:21:01 PM
 The inner loop is malformed.  

[pbcode]


      for temp2=1 to len(s$)-temp

         // IF / ELSE / ENDIF  
        if mid$(s$, temp2+temp, 1)=" "
            exitfor
          else
            inc dtemp

// WHERE IS THE END IF ??????????

      next temp2   

[/pbcode]

Title: Re: A smart word wrapper.
Post by: geecee on March 18, 2009, 12:52:55 AM
Very interesting ...... The programme still works with a malformed inner loop.

The programme does however have three ifs and three endifs.

:)
geecee
Title: Re: A smart word wrapper.
Post by: Big C. on March 18, 2009, 05:06:15 PM
Quote
The inner loop is malformed. 

Nice... I've point this in the 2nd Post ;)

Quote
The programme still works with a malformed inner loop.

I was also surprised at that. Can that be an error?
Title: Re: A smart word wrapper.
Post by: LemonWizard on April 27, 2009, 01:03:53 AM
I have no idea, most of the time when I'm doing programming I'm pretty tired and not all here =p
So, I tend to make a few mistakes.. overall the way I program is mostly through design first, trial and error.
And then, a working version.

Title: Re: A smart word wrapper.
Post by: geecee on April 27, 2009, 04:30:06 AM
Hi LemonWizard

Not an error on your part ....... A PB error

:)
geecee