home *** CD-ROM | disk | FTP | other *** search
/ The Programmer Disk / The Programmer Disk (Microforum).iso / xpro / tutor / pro12 / strings.bas < prev   
Encoding:
BASIC Source File  |  1990-10-15  |  3.4 KB  |  87 lines

  1. 10 'STRINGS.BAS - Demonstration program for working with strings
  2. 20 'From the GW-BASIC Tutorial Series (GWBT05, 10/15/1990)
  3. 30 '
  4. 40 'In this program we will use the following variables:
  5. 50 '
  6. 60 '     INPUTSTRING$   will be the string the user enters
  7. 70 '     LENGTH         will be the length of the string we are working with
  8. 80 '!    OUTPUT$        will be the part of the string we will work with
  9. 90 '
  10. 100 'Since it is possible to generate a LOT of errors if you ask for an invalid
  11. 110 'portion of a string (more characters than are in it, for example), we will
  12. 120 'check for errors BEFORE we ask BASIC to give us the requested part of the
  13. 130 'string...
  14. 140 '
  15. 150 'Start of main program:
  16. 160 KEY OFF
  17. 170 CLS
  18. 180 LOCATE 3,1,0
  19. 190 PRINT "STRINGS.BAS - Demonstration program for using strings in BASIC"
  20. 200 PRINT
  21. 210 PRINT "Before we can start, I need you to enter a string to work with.  You"
  22. 220 PRINT "may enter anything you want:  Words, numbers, sentences, and so on..."
  23. 230 PRINT "and I will convert whatever you put in into a string variable.  You"
  24. 240 PRINT "may press ENTER alone to quit..."
  25. 250 PRINT
  26. 260 LINE INPUT "What is your string => ";INPUTSTRING$
  27. 270 '
  28. 280 'To check for errors, we need to know the total length of the string, so
  29. 290 'we use the LEN command
  30. 300 '
  31. 310 LENGTH=LEN(INPUTSTRING$)
  32. 320 '
  33. 330 'If the LENGTH is zero, this means that the user did NOT put in a string,
  34. 340 'but just pressed enter, so we quit if LENGTH is zero...
  35. 350 '
  36. 360 IF LENGTH = 0 THEN END
  37. 370 '
  38. 380 'If the length is greater than zero, we continue...
  39. 390 PRINT
  40. 400 PRINT "Demonstrating LEFT$"
  41. 410 INPUT "How many characters should I show you from the LEFT SIDE";LEFTLENGTH
  42. 420 '
  43. 430 'If you ask for more characters than are there, this can generate an error,
  44. 440 'so let's check to be sure the request is OK...
  45. 450 '
  46. 460 IF LEFTLENGTH < LENGTH THEN GOTO 500
  47. 470 BEEP
  48. 480 PRINT "You asked for too many characters!  Only ";LENGTH;" are there!"
  49. 490 GOTO 400
  50. 500 OUTPUT$=LEFT$(INPUTSTRING$,LEFTLENGTH)
  51. 510 PRINT "The LEFT part you asked for is: ";OUTPUT$
  52. 520 PRINT
  53. 530 PRINT "Demonstrating RIGHT$"
  54. 540 INPUT "How many characters should I show you from the RIGHT SIDE";RIGHTLENGTH
  55. 550 '
  56. 560 'Again, if you ask for more than are there, this could be an error...
  57. 570 IF RIGHTLENGTH < LENGTH THEN GOTO 610
  58. 580 BEEP
  59. 590 PRINT "You asked for too many characters!  Only ";LENGTH;" are there!"
  60. 600 GOTO 530
  61. 610 OUTPUT$=RIGHT$(INPUTSTRING$,RIGHTLENGTH)
  62. 620 PRINT "The RIGHT part you asked for is: ";OUTPUT$
  63. 630 PRINT
  64. 640 PRINT "Demonstrating MID$"
  65. 650 INPUT "Where should I start giving you characters from the MIDDLE";START
  66. 660 INPUT "And how many characters would you like to see";MIDDLELENGTH
  67. 670 '
  68. 680 'Now, we need to check two things:  First, that the starting position is
  69. 690 'not greater than the total length of the string; and second, that the
  70. 700 'total number of characters needed is less than the total length of the
  71. 710 'string...
  72. 720 '
  73. 730 IF START < LENGTH THEN GOTO 770
  74. 740 BEEP
  75. 750 PRINT "Your starting position is too large!"
  76. 760 GOTO 650
  77. 770 IF START + MIDDLELENGTH < LENGTH THEN GOTO 810
  78. 780 BEEP
  79. 790 PRINT "You asked for more characters than are in the string!"
  80. 800 GOTO 650
  81. 810 OUTPUT$=MID$(INPUTSTRING$,START,MIDDLELENGTH)
  82. 820 PRINT "The MIDDLE part you asked for is: ";OUTPUT$
  83. 830 PRINT "Press any key to continue..."
  84. 840 WHILE INKEY$="":WEND
  85. 850 RUN
  86. 860 'End of program - STRINGS.BAS
  87.