QBASIC String Pattern C CO COM COMP COMPU COMPUT COMPUTE COMPUTER (RKL-WEB)

1 year ago

BASIC (Beginner's All-purpose Symbolic Instruction Code) has long been revered for its accessibility and ease of use, making it an ideal language for beginners to grasp fundamental concepts. In this blog post, we will explore a captivating BASIC program that unveils a visual journey by printing ascending substrings of a string. Here is the simple string manipulation pattern program which will draw the following output. 

C
CO
COM
COMP
COMPU
COMPUT
COMPUTE
COMPUTER

- CLS: clears the screen
- X$ = "COMPUTER": X$ string variable is declared and assigned the value "COMPUTER"
- FOR i = 1 TO LEN(X$)): Iterating from 1 to the length of the string X$
- PRINT LEFT$(X$, i): Within the loop, the LEFT$ function extracts a substring from the left side of the string X$, with the length determined by the loop variable i. Each iteration prints a progressively larger portion of the leftmost characters
- NEXT i: End of the loop
- END: End of the program

Program

CLS
X$ = "COMPUTER"
FOR i = 1 TO LEN(X$)
     PRINT LEFT$(X$, i)
NEXT i
END

 

Using SUB Procedure

DECLARE SUB pattern(a$)
CLS
x$ = "COMPUTER"
CALL pattern(x$)
END

SUB pattern (a$)
FOR i = 1 TO LEN(a$)
     PRINT LEFT$(a$, i)
NEXT i
END SUB

 

Using FUNCTION Procedure

DECLARE FUNCTION pattern()
CLS
d = pattern(x$)
END

FUNCTION pattern (a$)
a$ = "COMPUTER"
FOR i = 1 TO LEN(a$)
     PRINT LEFT$(a$, i)
NEXT i
END FUNCTION
  911
Please Login to Post a Comment