Level:
Correct: 0
Incorrect: 0
Marks: 0/0
Exam Practice
Subject:
Topics:
Avg
score:
–
Topics
done:
0/32

String handling

Practise 29 exam-style questions on String handling for Cambridge IGCSE Computer Science (Paper 2). Write your answer as you would in the exam and get it marked in seconds against the mark scheme, point by point, with an AI tutor on hand if you get stuck.

  • 29 questions
  • 1–6 marks each
  • Instant marking
  • No sign-up needed
20 questions, marked as you go

Sample questions with model answers

Here are 4 of the questions in this topic, each with a model answer, an explanation of why it earns the marks and the mark scheme an examiner would use.

Question 1

1 mark

The variable City stores the string "PARIS".

Write a pseudocode statement that stores the contents of City in lower case in the variable LowerCity.

Model answer

LowerCity ← LCASE(City)

Explanation

LCASE returns a copy of the string with every upper-case letter changed to lower case. The original value in City is not changed; the result is assigned to LowerCity with ←.

Mark scheme
  1. 1 mark

    LowerCity ← LCASE(City)

    Do not accept LowerCity ← "paris" or = in place of ←.

Question 2

2 marks

Write a pseudocode statement that stores the last character of the string in Word in the variable LastChar. The statement must work for a word of any length.

Model answer

LastChar ← SUBSTRING(Word, LENGTH(Word), 1)

Explanation

The last character is at the position equal to the length of the string, because the first character is at position 1. Using LENGTH(Word) instead of a fixed number makes the statement work for any word.

Mark scheme
  1. 1 mark

    MP1 SUBSTRING used to return 1 character, assigned to LastChar

  2. 1 mark

    MP2 start position is LENGTH(Word)

    Do not accept a fixed number as the start position.

Question 3

3 marks

Write pseudocode to input a person's first name and surname, then output their initials in upper case.

For example, the inputs amira and khan output AK.

Model answer

INPUT FirstName
INPUT Surname
OUTPUT UCASE(SUBSTRING(FirstName, 1, 1)), UCASE(SUBSTRING(Surname, 1, 1))

Explanation

SUBSTRING with a start position of 1 and a length of 1 returns the first character of a string. UCASE makes sure the initials are capitals whatever the user typed. OUTPUT with a comma between the values prints them next to each other.

Mark scheme
  1. 1 mark

    MP1 inputs the first name and the surname

  2. 1 mark

    MP2 extracts the first character of each name using SUBSTRING(..., 1, 1)

  3. 1 mark

    MP3 converts both initials to upper case and outputs them together

Question 4

5 marks

Write a pseudocode function CountChar that takes a string Text and a character Target as parameters. It returns the number of times Target appears in Text, ignoring case.

For example, CountChar("Banana", 'A') returns 3.

Model answer

FUNCTION CountChar(Text : STRING, Target : CHAR) RETURNS INTEGER
    DECLARE Count : INTEGER
    DECLARE Position : INTEGER
    Count ← 0
    FOR Position ← 1 TO LENGTH(Text)
        IF LCASE(SUBSTRING(Text, Position, 1)) = LCASE(Target)
          THEN
            Count ← Count + 1
        ENDIF
    NEXT Position
    RETURN Count
ENDFUNCTION

Explanation

The function works on its parameters, not on values typed in, and gives back its answer with RETURN. Converting both the character from the string and the target to lower case makes the comparison ignore case, so the B-a-n-a-n-a example counts all three a's when 'A' is passed.

Mark scheme
  1. 1 mark

    MP1 function header with the two parameters and RETURNS INTEGER, ended with ENDFUNCTION

    max 5; e.g. FUNCTION CountChar(Text : STRING, Target : CHAR) RETURNS INTEGER

  2. 1 mark

    MP2 counter set to 0 inside the function, before the loop

  3. 1 mark

    MP3 loop from 1 to LENGTH(Text)

  4. 1 mark

    MP4 extracts each character with SUBSTRING(Text, Position, 1)

  5. 1 mark

    MP5 converts both the character and Target to the same case before comparing

  6. 1 mark

    MP6 adds 1 to the counter when they match

  7. 1 mark

    MP7 returns the count after the loop

    Do not accept OUTPUT in place of RETURN.

Ready to try the rest? Every answer you write is marked against the mark scheme, so you can see exactly where the marks are.

AI Tutor Hints first, answers after you try

Hi! Stuck? Ask me for a hint and I’ll help you work it out. Once you’ve checked your answer I can explain exactly where the marks are.