home *** CD-ROM | disk | FTP | other *** search
/ Power-Programmierung / CD1.mdf / magazine / pcmagazi / 1992 / 13 / strbrk.bas < prev    next >
BASIC Source File  |  1992-01-25  |  2KB  |  56 lines

  1. DEFINT A-Z
  2. DECLARE FUNCTION StringBreak% (Work$, Wide, Array$())
  3.  
  4. REDIM Array$(1 TO 1)                    'establish the string array
  5.  
  6. Work$ = "The quick brown fox doesn't mind getting broken down"
  7. Work$ = Work$ + " into shorter lines at all, as you can tell."
  8. Wide = 40
  9. NumElements = StringBreak%(Work$, Wide, Array$())
  10.  
  11. CLS
  12. PRINT STRING$(80, 205);                 'print a ruler
  13. FOR X = 1 TO 8: PRINT SPC(8); STR$(X); : NEXT
  14. FOR X = 1 TO 8: PRINT "1234567890"; : NEXT
  15. PRINT STRING$(80, 205);
  16.  
  17. FOR X = 1 TO NumElements
  18.   PRINT TAB(11); Array$(X)              'TAB indents the text
  19. NEXT
  20.  
  21. FUNCTION StringBreak% (Work$, Wide, Array$()) STATIC
  22.  
  23.   Elements = LEN(Work$) \ Wide + 5      'estimate the array size
  24.   REDIM Array$(1 TO Elements)           'establish that many elements
  25.   Length = LEN(Work$)                   'determine this once
  26.  
  27.   CurEl = 1                             'start at element 1
  28.   CurCol = 1                            'and at column 1
  29.  
  30.   DO
  31.  
  32.     Found = 0                           'assume we won't find a space
  33.  
  34.     FOR X = CurCol + Wide TO CurCol STEP -1         'search for a space
  35.       IF MID$(Work$, X, 1) = " " OR X = Length THEN 'found one, or at end
  36.         Array$(CurEl) = MID$(Work$, CurCol, X - CurCol - (X = Length))
  37.         CurCol = X + 1                              'update the column
  38.         Found = -1                                  'show we found it
  39.         EXIT FOR                                    'done with this element
  40.       END IF
  41.     NEXT
  42.  
  43.     IF NOT Found THEN                               'no place to break it
  44.       Array$(CurEl) = MID$(Work$, CurCol, Wide)     'so force a break
  45.       CurCol = CurCol + Wide                        'update the column
  46.     END IF
  47.  
  48.     CurEl = CurEl + 1                               'go to next element
  49.  
  50.   LOOP UNTIL CurCol >= Length                       'while there's more
  51.  
  52.   StringBreak% = CurEl - 1                          'assign the function
  53.  
  54. END FUNCTION
  55.  
  56.