Write a program in qbasic to count the number of words present in the given sentences using SUB procedure
Answers
Answer:
QBASIC PROGRAM TO COUNT TOTAL NO. OF WORDS IN SENTENCE(FUNCTION)
DECLARE FUNCTION COUNT (S$)
CLS
INPUT "ENTER ANY STRING"; S$
PRINT "TOTAL NO. OF WORDS= "; COUNT(S$)
END
FUNCTION COUNT (S$)
WC = 1
FOR I = 1 TO LEN(S$)
B$ = MID$(S$, I, 1)
IF B$ = " " THEN
WC = WC + 1
END IF
NEXT I
COUNT = WC
END FUNCTION
Answer:
CLS
'PROGRAM TO COUNT THE NO. OF WORDS IN A SENTENCE
INPUT "Enter a sentence";a$
a = 1
FOR i= 1 TO LEN(a$)
IF MID$(a$,i,1)=" " THEN
a=a+1
END IF
NEXT i
PRINT "The no. of words in sentence is";a
END
Explanation:
This is a easy method of the program, as MID$(a$,i,1) carries a single letter from the sentence.During this if it gets the value true i.e.(a single space), it will increase the value of 'a' by 1.
(It's a common sense that to separate a word in a sentence, we use a space)