A text analysis program needs to calculate statistics for a sentence entered by the user. The program should count: The total number of characters (includin...

Assessment: Computer Science 0478 | Paper 2 Mock 01 | Algorithms, Programming and Logic Subject: Computer Science - 0478

Question 1 Report

A text analysis program needs to calculate statistics for a sentence entered by the user. The program should count:

  • The total number of characters (including spaces)
  • The number of letters (excluding spaces and punctuation)
  • The number of spaces
  • The length of the longest word

(a) Write pseudocode for this algorithm. [4]

Answer Details

(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: ", LongestWord

The 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].

Download The App On Google Playstore

Everything you need to excel in your exams

Green Bridge CBT Mobile App
Personalized AI Learning Chat Assistant
200,000+ Exam Questions Across IGCSE, JAMB, WAEC & NECO
Over 3,900 Lesson Notes
Offline Support - Learn Anytime, Anywhere
Green Bridge Timetable
Literature Summaries & Potential Questions
Track Your Performance & Progress
In-depth Explanations for Comprehensive Learning