Main Menu

A smart word wrapper.

Started by LemonWizard, March 14, 2009, 02:54:10 AM

Previous topic - Next topic

LemonWizard

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.

PlayBASIC Code: [Select]
; 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








Big C.

#1
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 :)

PlayBASIC Code: [Select]
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



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

PlayBASIC Code: [Select]
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





geecee

#2
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.


PlayBASIC Code: [Select]
;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



:)
geecee

LANG MEY YER LUM REEK

A smile costs less than electricity and gives more light :)

LemonWizard

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?

geecee

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

LANG MEY YER LUM REEK

A smile costs less than electricity and gives more light :)

kevin

#5
 The inner loop is malformed.  

PlayBASIC Code: [Select]
       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





geecee

Very interesting ...... The programme still works with a malformed inner loop.

The programme does however have three ifs and three endifs.

:)
geecee
LANG MEY YER LUM REEK

A smile costs less than electricity and gives more light :)

Big C.

#7
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?

LemonWizard

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.


geecee

Hi LemonWizard

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

:)
geecee
LANG MEY YER LUM REEK

A smile costs less than electricity and gives more light :)