QBASIC program to count the total number of vowel letters from input string
Introduction
Steps to create a QBASIC program to count vowel letters
- Start by opening the QBASIC editor.
- Create a new program and give it a name.
- Declare a variable to store the input string.
- Ask the user to input a string.
- Initialize a counter variable to 0.
- Loop through each character in the string.
- If the character is a vowel letter, increment the counter variable.
- Print the total number of vowel letters to the screen.
- End the program.
Code
Explanation of Code:
- CLS: This command clears the screen.
- INPUT "Enter any string:"; s$: This statement prompts the user to enter a string and stores the string in the variable "s$".
- l = LEN(s$): This statement calculates the length of the input string and stores the value in the variable "l".
- FOR x = 1 TO l: This statement sets up a FOR loop that will iterate through each character of the input string.
- m$ = UCASE$(MID$(s$, x, 1)): This statement extracts each character of the input string and stores it in the variable "m$". The UCASE$ function converts the character to uppercase to make it easier to check if it is a vowel.
- IF m$ = "A" OR m$ = "E" OR m$ = "I" OR m$ = "O" OR m$ = "U" THEN: This line of code checks if the character is a vowel by comparing it to the letters "A", "E", "I", "O", and "U".
- v = v + 1: If the current character is a vowel, the program increments the variable "v" (which represents the count of vowel letters) by 1.
- ELSE: If the current character is not a vowel, the program increments the variable "c" (which represents the count of consonant letters) by 1.
- NEXT x: This statement ends the FOR loop and moves to the next character of the input string.
- PRINT "Total number of vowel letters="; v: This statement prints the total number of vowel letters by displaying the message "Total number of vowel letters=" followed by the value of the variable "v".
- END: This statement ends the program.
By using SUB procedure (SUB…END SUB)
DECLARE SUB vowel (s$)
CLS
INPUT
"Enter any string:"; s$
CALL
vowel(s$)
END
SUB
vowel (s$)
l
= LEN(s$)
FOR
x = 1 TO l
m$
= UCASE$(MID$(s$, x, 1))
IF
m$ = "A" OR m$ = "E" OR m$ = "I" OR m$ =
"O" OR m$ = "U" THEN
v
= v + 1
ELSE
c
= c + 1
END
IF
NEXT
x
PRINT
"Total number of vowel letters="; v
END
SUB
By
using FUNCTION procedure (FUNCTION..END FUNCTION)
DECLARE FUNCTION vowel (s$)
CLS
INPUT
"Enter any string:"; s$
PRINT
"Total number of consonants letters="; vowel(s$)
END
FUNCTION
vowel (s$)
l
= LEN(s$)
FOR
x = 1 TO l
m$
= UCASE$(MID$(s$, x, 1))
SELECT
CASE m$
CASE
"A", "E", "I", "O", "U"
v$
= v$ + m$
CASE
ELSE
c$
= c$ + m$
END
SELECT
NEXT
x
vowel
= LEN(v$)
END
FUNCTION
No comments:
Post a Comment