How do I print a string in reverse order in QBasic?

How do I print a string in reverse order in QBasic?


In QBasic, you can use a "FOR" loop to iterate through the characters of a string in reverse order and print them one by one. Here's an example of a simple program that will print a string in reverse order:


INPUT "Enter a string: ", s$

FOR i = LEN(s$) TO 1 STEP -1

    PRINT MID$(s$, i, 1);

NEXT i


In this example, the "INPUT" statement prompts the user to enter a string, which is stored in the variable "s$". The "FOR" loop then starts at the last character of the string (using the "LEN" function to determine the string's length) and ends at the first character, counting backwards by -1.

The "MID$" function is used to retrieve the character at the current position of the loop (i-th position) and print it.

Post a Comment

0 Comments