QBASIC Program to display the input string in reverse order
Introduction
QBASIC is a high-level programming language that was popular in the 1980s and 1990s. It was used to create small applications and games for personal computers. In this tutorial, we will learn how to write a QBASIC program to display an input string in reverse order. This program will take a string as input and output the same string in reverse order. This tutorial assumes that you have some basic knowledge of QBASIC programming.
Steps to Create a QBASIC Program to Display Input String in Reverse Order
Step 1: Take Input from User
We need to take input from the user. To do this, use the INPUT statement followed by the variable name. For example, INPUT "Enter a string: ", b$ will display the message "Enter a string: " and then store the user input in the b$ variable.
Step 2: Reverse the Input String
To reverse the input string, we need to loop through each character of the string in reverse order and add it to the output string variable. We can do this using a FOR loop that starts from the last character of the input string and ends at the first character. Inside the loop, we can use the MID$ function to get each character of the input string and add it to the output string. For example:
FOR x = LEN(b$) TO 1 STEP -1
c$ = c$ + MID$(b$, x, 1)
NEXT
Step 3: Display the Output String
Finally, we need to display the output string. To do this, use the PRINT statement followed by the c$ variable. For example, PRINT "Reverse string: "; c$ will display the message "Reverse string: " followed by the c$.
Full QBASIC Program
The above program can be solved by using SUB and FUNCTION procedure also.
By using SUB procedure (SUB.....END SUB)
DECLARE SUB rev (b$)
CLS
INPUT "Enter string"; b$
CALL rev(b$)
END
SUB rev (b$)
FOR x = LEN(b$) TO 1 STEP -1
c$ = c$ + MID$(b$, x, 1)
NEXT
PRINT c$
END SUB
By using FUNCTION procedure (FUNCTION.....END FUNCTION)
DECLARE FUNCTION reverse$ (b$)
CLS
INPUT "Ente any string:"; b$
PRINT reverse$(b$)
END
FUNCTION reverse$ (b$)
FOR x = LEN(b$) TO 1 STEP -1
c$ = c$ + MID$(b$, x, 1)
NEXT
reverse$ = c$
END FUNCTION
Output
Conclusion
In this tutorial, we learned how to write a QBASIC program to display an input string in reverse order. We defined variables to hold the input and output strings, took input from the user, looped through the input string to reverse it, and displayed the output string. QBASIC may not be as popular as it once was, but it still has its uses for small projects and games.
FAQs
1) What is QBASIC?
Ans: QBASIC is a high-level programming language that was popular in the 1980s and 1990s.
2) How do I open QBASIC?
Ans:You can find QBASIC in the Start menu or by searching for it in the search bar.
3) Can I use QBASIC for larger projects?
Ans: QBASIC is not recommended for larger projects as it has limited capabilities compared to modern programming languages.
4) Can I use this program to reverse a sentence?
Ans: No, this program only reverses a string character by character. To reverse a sentence, you would need to split the sentence into words and then reverse the order of the words.
5) Are there any other programming languages that can reverse a string?
Ans: Yes, almost all programming languages have the capability to reverse a string. The syntax may vary between languages, but the logic is the same.
No comments:
Post a Comment