Question 5(b)(i) 2019 · May-June · Paper 23 · 3 marks
Reveal official mark-scheme answer
Official answer for Question 5
Part (b)
(i)
Question 3
- three possible formats for string containg two words:
- When a space character is encountered
- NumWords is incremented by 1
- The algorithm counts the spaces
- and not the words
Show text version of question
Question 5
Part (b)
(i)
Question 5
Nigel is learning about string handling. He wants to write code to count the number of words in a
given string. A word is defined as a sequence of alphabetic characters that is separated by one or more space characters.
His first attempt at writing an algorithm in pseudocode is as follows:
PROCEDURE CountWords(Message : STRING)
DECLARE NumWords : INTEGER DECLARE Index : INTEGER CONSTANT Space = ' '
NumWords 0
FOR Index 1 TO LENGTH(Message) IF MID(Message, Index, 1) = Space THEN NumWords NumWords + 1 ENDIF ENDFOR
OUTPUT "Number of words : " , NumWords
ENDPROCEDURE
For the built-in functions list, refer to the Appendix on page 18.
His first attempt is incorrect. He will use white-box testing to help him to identify the problem.
(i)
Write a test string containing two words that gives the output:
Number of words : 2
Use the symbol '∇' to represent each space character in your test string.
Explain why the algorithm gives the output shown above.
String .................................................................................................................................
Explanation .......................................................................................................................
...........................................................................................................................................
...........................................................................................................................................
...........................................................................................................................................
...........................................................................................................................................
Why this question appears here
82% assigned-topic match · Hardware › Logic gates and logic circuits
Topic-match percentages estimate how closely each question fits the selected syllabus topic. They are a guide for choosing stricter or broader practice.
Permanent source page: Educate A Change · From 9608_s19_qp_23.pdf
