LearnStanmorephysics

Grade 11 · Powering Up

Copy, Pos and building new strings

Grade 11 · String manipulation · Lesson and coding task · about 14 minutes

Open this task in the Delphi workspace →

Copy cuts a piece out of a string; Pos tells you where to cut. Together they dismantle structured text — names, codes, ID numbers, file lines:

Copy(sText, start, count)     // count characters from position start
Pos(' ', sFull)               // where is the first space?

Classic move — split a full name at the space:

iSpace   := Pos(' ', sFull);
sName    := Copy(sFull, 1, iSpace - 1);
sSurname := Copy(sFull, iSpace + 1, Length(sFull));

(Asking Copy for more characters than remain is safe — it just takes what is there.) Two more tools that change a string variable directly: Delete(s, start, count) and Insert(sub, s, position).

Your task

Turn a full name into neat initials: Thabo Mokoena → T.M. — capitals, whatever case was typed.

What you will do

Hints
  1. The first initial is simply Copy(sFull, 1, 1) — or sFull[1].
  2. The surname starts one position after the space: Copy(sFull, iSpace + 1, 1).
  3. Build 'T' + '.' + 'M' + '.' and UpperCase the lot.
Model solution
procedure TfrmInitials.btnInitialsClick(Sender: TObject);
var
  sFull, sInitials : String;
  iSpace : Integer;
begin
  sFull := edtName.Text;
  iSpace := Pos(' ', sFull);
  sInitials := Copy(sFull, 1, 1) + '.' + Copy(sFull, iSpace + 1, 1) + '.';
  sInitials := UpperCase(sInitials);
  lblInitials.Caption := sInitials;
end;

About this material

This platform provides original CAPS-aligned practice material and study tools. Content is machine-verified and has not been reviewed by subject specialists. It is not affiliated with or endorsed by the Department of Basic Education. Learners should also use official past papers and consult their teachers where uncertain.