ITPEC FE Subject B April 2026 Question 13

From the answer group below, select the correct answer to be inserted into blank in the program. Here, the array indexes start at 1.

The function extractEmails receives text as an argument, extracts email addresses from the text, and returns all collected email addresses. Here, a string in the format “name@domain” is considered to be an email address, where “name” and “domain” comprise only alphanumeric characters and the characters “.” (dot), “_” (underscore), and “-” (hyphen-minus). The table shows the description of the functions used in the function extractEmails.

Table Functions used in the function extractEmails

FunctionReturn valueDescription
findSubstring(string: str,string: substr,integer: start)integerReturns the position of the first occurrence of the substring substr in the string str, starting from the position start. If the substring is not found, it returns 0.
getChar(string: str,integer: index)characterReturns the character at the position specified by index in the string str.
length(string: str)integerReturns the length of the string str.

extractEmails("Contact us at john.doe@example.com or jane_smith@test.org for support.") returns {"john.doe@example.com", "jane_smith@test.org"}. In addition, extractEmails("a@b@c") returns {"a@b", "b@c"}.

[Program]
○ string []: extractEmails(string: txt)
character []: validChars ← {alphanumeric characters, ".", "_", "-"}
string []: results ← {}
integer: index ← 1
integer: atIndex, i, count
string: candidate
while (true)
atIndex ← findSubstring(txt, "@", index)
if (atIndex = 0)
exit the while block
endif
candidate ← ""
for (blank by 1)
if (getChar(txt, i) exists in validChars)
prepend getChar(txt, i) to candidate
else
exit the for block
endif
endfor
count ← 0
if (length(candidate) > 0)
append "@" to candidate
for (increase i from atIndex + 1 to length(txt) by 1)
if (getChar(txt, i) exists in validChars)
append getChar(txt, i) to candidate
count ← count + 1
else
exit the for block
endif
endfor
if (count > 0)
append the value of candidate at the end of results
endif
endif
index ← atIndex + 1
endwhile
return results

Answer group

Explanation

decrease i from atIndex - 1 to index — the name part lies left of the "@" and is built with prepend, so it is read right to left.

The loop collects the name before the "@" found at atIndex:

  • •it starts one character before the "@", at atIndex - 1, since "@" itself is not in validChars
  • •it moves backwards and stops at index, one past the previous "@", or earlier at the first invalid character such as a space
  • •each character is prepended, so reading right to left rebuilds the name in its original order

Trace of "a@b@c": the first "@" is at 2, the loop reads position 1 and gives "a", then the domain loop adds "b", so "a@b" is stored. Now index = 3 and the next "@" is at 4; the loop reads position 3 only, giving "b", then "b@c". This matches the stated result {"a@b", "b@c"}.

Answer (a)

Why not others:

  • •(b) — starts at atIndex, the "@" itself; it is not a valid character, so the loop exits at once, candidate stays empty and no address is ever collected
  • •(c) and (d) — walk forwards from index while prepending, which reverses the name; they also start at the beginning of the text instead of at the "@", so in the first example they read "Contact", stop at the space, and store "tcatnoC@example.com"

Key rule: Match the direction of the scan to how the string is built: prepend while moving left, append while moving right. Start the scan next to the anchor character, not on it.

String Processing · AI-written, may contain errors

This question comes from an official ITPEC past paper. ITPEC Practice is an independent study tool and is not affiliated with ITPEC. See the official FE past-paper collection or Report an issue.