Question 1 Report
A text analysis program needs to calculate statistics for a sentence entered by the user. The program should count:
(a) Write pseudocode for this algorithm. [4]
(a) The algorithm analyses a sentence to count total characters, letters only, spaces, and the length of the longest word. [4]
DECLARE Sentence : STRING
DECLARE TotalChars : INTEGER
DECLARE LetterCount : INTEGER
DECLARE SpaceCount : INTEGER
DECLARE LongestWord : INTEGER
DECLARE CurrentWordLen : INTEGER
DECLARE i : INTEGER
DECLARE Ch : CHAR
INPUT Sentence
TotalChars ← LENGTH(Sentence)
LetterCount ← 0
SpaceCount ← 0
LongestWord ← 0
CurrentWordLen ← 0
FOR i ← 1 TO TotalChars
Ch ← Sentence[i]
IF Ch = ' ' THEN
SpaceCount ← SpaceCount + 1
IF CurrentWordLen > LongestWord THEN
LongestWord ← CurrentWordLen
ENDIF
CurrentWordLen ← 0
ELSE
IF (Ch >= 'A' AND Ch <= 'Z') OR (Ch >= 'a' AND Ch <= 'z') THEN
LetterCount ← LetterCount + 1
CurrentWordLen ← CurrentWordLen + 1
ENDIF
ENDIF
NEXT i
IF CurrentWordLen > LongestWord THEN
LongestWord ← CurrentWordLen
ENDIF
OUTPUT "Total characters: ", TotalChars
OUTPUT "Letters: ", LetterCount
OUTPUT "Spaces: ", SpaceCount
OUTPUT "Longest word length: ", LongestWordThe total character count comes directly from LENGTH(Sentence) [1]. The loop checks each character: spaces increment the space counter and trigger a longest-word check, while letters increment both the letter counter and the current word length [1]. After the loop, a final check compares the last word (which has no trailing space) against the longest found so far [1].
Everything you need to excel in your exams