Computer for SEE and NEB

It is a complete SEE and NEB solution for computer science. It includes Computer Fundamentals, Database (SQL), Programming in C QBASIC, CSS, JavaScript, and PHP for beginners.

Breaking

Post Top Ad

Your Ad Spot

Friday, December 9, 2022

Counting Consonants Made Easy with QBASIC Program to Count Total Number of Consonant Letters from Input String

 Counting Consonants Made Easy with QBASIC Program to Count Total Number of Consonant Letters from Input String

 


QBASIC Program to Count Total Number of Consonant Letters from Input String


Introduction:


Have you ever needed to count the total number of consonant letters in a given string? It can be a daunting task, especially if the string is long and complex. Fortunately, with QBASIC program, you can easily and accurately count the total number of consonants in any string. In this article, we will guide you through the process of creating a QBASIC program to count the total number of consonant letters from an input string.

What are Consonant Letters?


Before diving into the program, it's important to understand what consonant letters are. Consonant letters are those letters of the English alphabet that are not vowels. These letters include B, C, D, F, G, H, J, K, L, M, N, P, Q, R, S, T, V, W, X, Y, and Z.

Understanding the QBASIC Program to Count Total Number of Consonant Letters from Input String

Now that we understand what consonant letters are, let's take a look at how the QBASIC program works. The program takes an input string from the user and then counts the total number of consonant letters in that string. Here's how the program works:
  1. The user is prompted to enter a string.
  2. The program then converts the entire string to lowercase. This is done to ensure that all the consonant letters are correctly identified.
  3. The program then loops through each character in the string.
  4. For each character, the program checks if it is a consonant letter.
  5. If the character is a consonant letter, the program increments a counter by 1.
  6. Once all characters have been checked, the program displays the total number of consonant letters in the input string.

Code for the Program:




Explanation:

  1. The CLS statement clears the screen.
  2. The INPUT statement prompts the user to enter any string, and stores the input string in the variable s$.
  3. The LEN function is used to find the length of the input string, which is stored in the variable l.
  4. The FOR loop is used to loop through each character in the input string.
  5. The MID$ function is used to extract a single character from the input string, starting at the current loop index (x).
  6. The UCASE$ function is used to convert the extracted character to uppercase, so that we can easily compare it with the vowels.
  7. 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.
  8. The NEXT statement is used to move on to the next character in the input string.
  9. 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.
We can solve the above program by using SUB and FUNCTION procedure

 

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


Output:




FAQs


Q: What is QBASIC? 
Ans: QBASIC is a programming language that was created by Microsoft in the 1990s. It is a simple, easy-to-learn language that is still popular among beginners and hobbyists.

Q: Can I use QBASIC to create other programs? 
Ans: Yes, QBASIC can be used to create a wide range of programs, including games, utilities, and applications.

Q: Can I modify the QBASIC program to count vowels instead of consonants? 
Ans: Yes, you can modify the program to count vowels instead of consonants. Simply change the INSTR statement to check for vowel letters ("aeiou") instead of consonant letters.

 Conclusion


Counting the total number of consonant letters in a given string can be a time-consuming and error-prone task. However, with the QBASIC program to count total number of consonant letters from input string, you can quickly and accurately count the consonant letters in any string. By following the step-by-step guide and tips outlined in this article, you can create a program that's both efficient and easy to use.



No comments:

Post a Comment

Post Top Ad

Your Ad Spot

Pages