Counting Consonants Made Easy with QBASIC Program to Count Total Number of Consonant Letters from Input String
Introduction:
What are Consonant Letters?
Understanding the QBASIC Program to Count Total Number of Consonant Letters from Input String
- The user is prompted to enter a string.
- The program then converts the entire string to lowercase. This is done to ensure that all the consonant letters are correctly identified.
- The program then loops through each character in the string.
- For each character, the program checks if it is a consonant letter.
- If the character is a consonant letter, the program increments a counter by 1.
- Once all characters have been checked, the program displays the total number of consonant letters in the input string.
Code for the Program:
Explanation:
- The CLS statement clears the screen.
- The INPUT statement prompts the user to enter any string, and stores the input string in the variable s$.
- The LEN function is used to find the length of the input string, which is stored in the variable l.
- The FOR loop is used to loop through each character in the input string.
- The MID$ function is used to extract a single character from the input string, starting at the current loop index (x).
- The UCASE$ function is used to convert the extracted character to uppercase, so that we can easily compare it with the vowels.
- The IF statement is used to check if the extracted character is a vowel. If it is, the character is added to the v$ string, which stores all the vowel letters in the input string. If the extracted character is not a vowel, it is added to the c$ string, which stores all the consonant letters in the input string.
- The NEXT statement is used to move on to the next character in the input string.
- Finally, the PRINT statement is used to display the total number of consonant letters in the input string, which is equal to the length of the c$ string.
By
using the SUB procedure (SUB…END SUB)
DECLARE SUB cons (s$)
CLS
INPUT
"Enter any string:"; s$
CALL
cons(s$)
END
SUB
cons (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$ + m$
ELSE
c$
= c$ + m$
END
IF
NEXT
x
PRINT
"Total consonants letters="; LEN(c$)
END
SUB
By
using the FUNCTION procedure (FUNCTION..END FUNCTION)
DECLARE FUNCTION cons (s$)
CLS
INPUT
"Enter any string:"; s$
PRINT
"Total consonants letters="; cons(s$)
END
FUNCTION
cons (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
cons
= LEN(c$)
END FUNCTION
No comments:
Post a Comment