QBASIC program to count the total number of vowels from input string
'
Introduction:
QBASIC is a high-level programming language used for developing computer applications. It is widely used for teaching programming to beginners due to its simple syntax and easy-to-understand structure. In this article, we will guide you through writing a QBASIC program to count the total number of vowels from an input string. This program can be useful in various applications, such as text analysis, data mining, and natural language processing.
Understanding Vowels
Before we dive into the programming aspect, let's briefly understand what vowels are. In English, there are five vowels: A, E, I, O, and U. These letters can appear in both uppercase and lowercase forms, and they can appear multiple times in a word or sentence.
Writing the Program
Now that we have a plan, let's start writing the program. Here's the QBASIC code:
Explanation of the program
- First, the program clears the screen using the "CLS" command to make the output easier to read.
- Next, the program prompts the user to enter a string using the "INPUT" statement and assigns the input to the variable "a$".
- Then, the program uses a "FOR" loop to iterate through each character in the input string. The loop runs from 1 to the length of the input string using the "LEN" function to get the length of the string.
- Inside the loop, the program uses the "MID$" function to extract each character of the input string one by one, starting from the first character. The "UCASE$" function is then used to convert the character to uppercase to make the program case-insensitive.
- The program then uses a "SELECT CASE" statement to check if the character is a vowel. If the character is a vowel (either "A", "E", "I", "O", or "U"), then the program increments a variable called "v" by 1 using the "v = v + 1" statement.
- Finally, after the loop completes, the program displays the total number of vowels found in the input string using the "PRINT" statement along with the value of the variable "v".
Overall, the program uses a loop and conditional statements to iterate through the input string and count the number of vowels found in it.
We can solve above program by using SUB and FUNCTION procedures.
By using SUB procedure (SUB…END SUB)
DECLARE SUB vowel (a$)
CLS
INPUT "Enter any string"; a$
CALL vowel(a$)
END
SUB voel (a$)
FOR i = 1 TO LEN(a$)
m$ = UCASE$(MID$(a$, i, 1))
IF m$ = "A" OR m$ = "E" OR m$ = "I" OR m$ = "O" OR m$ = "U" THEN
v = v + 1
END IF
NEXT i
PRINT "Total vowel letters:"; v
END SUB
By using FUNCTION procedure (FUNCTION….END FUNCTION)
DECLARE FUNCTION vowel (a$)
CLS
INPUT "Enter any string:"; a$
PRINT "Total vowel letters:"; vowel(a$)
END
FUNCTION vowel (a$)
FOR i = 1 TO LEN(a$)
m$ = UCASE$(MID$(a$, i, 1))
SELECT CASE m$
CASE "A", "E", "I", "O", "U"
v$ = v$ + m$
CASE ELSE
c$ = c$ + m$
END SELECT
NEXT i
vowel = LEN(v$)
END FUNCTION
Output:
FAQs
Q: Why do we convert the input string to lowercase?
Ans: We convert the input string to lowercase to make the program case-insensitive. This means that the program will count both uppercase and lowercase vowels in the input string.
Q: What happens if the user enters a non-string input?
Ans: QBASIC will throw an error if the user enters a non-string input. You can add error handling code to handle such cases.
Q: Can we modify the program to count only uppercase or lowercase vowels?
Ans: Yes, you can modify the program to count only uppercase or lowercase vowels by modifying the "SELECT CASE" statement.
Conclusion
In conclusion, we have learned how to write a QBASIC program to count the total number of vowels in an input string. By following the steps outlined in this article, you can create an efficient and error-free program. We hope this article has been helpful in understanding the basics of QBASIC programming and the importance of planning before coding.
No comments:
Post a Comment