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

Wednesday, December 14, 2022

QBASIC program to count total number of words from input string


QBASIC program to count the total number of words from input string




Introduction:


Are you looking for a way to count the total number of words in an input string using QBASIC program? Look no further! In this article, we will guide you through the process of creating a QBASIC program to count the number of words in any given input string. Whether you're a beginner or an experienced QBASIC programmer, this guide is perfect for you.

Understanding the Problem


Before we start writing the QBASIC program, we need to understand the problem at hand. The task is to write a program that takes an input string from the user and counts the total number of words in that string. A word is defined as a sequence of characters separated by spaces.

Step-by-Step Guide to Writing the QBASIC Program


Now that we understand the problem, let's start with writing the QBASIC program. The following are the steps we will take to write the program:

Step 1: Accepting the Input String
Step 2: Initializing the Counter
Step 3: Counting the Words
Step 4: Displaying the Result

Writing program (Coding)

CLS
INPUT "Enter any string"; w$
FOR x = 1 TO LEN(w$)
m$ = MID$(w$, x, 1)
IF m$ = " " THEN w = w + 1
NEXT x
PRINT "Total word in string="; w + 1
END

Explanation of the program:

  • The program starts by clearing the screen using the command CLS.
  • Then, the program prompts the user to enter any string using the INPUT statement and stores the input in a variable called w$.
  • After that, the program runs a loop from 1 to the length of the input string (LEN(w$)). Inside the loop, it uses the MID$ function to extract each character of the string one by one and store it in a variable called m$.
  • If the character is a space (" "), it means a word has ended, so the program increments the word counter variable (w) by 1.
  • After the loop is completed, the program prints the total number of words in the input string by adding 1 to the value of the word counter variable (w + 1).
  • Finally, the program ends using the END statement.

We can write the above program using SUB and FUNCTION procedure



Output:



FAQs


1) What is QBASIC? 
Ans: QBASIC is a high-level programming language that is used for developing software applications and games.

2) How do I run a QBASIC program? 
Ans: To run a QBASIC program, you need to have a QBASIC compiler installed on your computer. You can then use the compiler to compile and run the program.

3) Can I use QBASIC to develop web applications? 
Ans: No, QBASIC is not suitable for developing web applications. It is mainly used for developing desktop applications and games.

4) Is QBASIC easy to learn? 
Ans: Yes, QBASIC is easy to learn and use. It is a beginner-friendly programming language that is great for people who are new to programming.

Conclusion

In this article, we have discussed how to write a QBASIC program to count the total number of words from an input string. We have provided a step-by-step guide to writing the program, which includes accepting the input string, initializing the counter, counting the words, and displaying the result. By following these steps, you can easily write a QBASIC program to count the total number of words from any input string.


No comments:

Post a Comment

Post Top Ad

Your Ad Spot

Pages