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
- Find the space with Pos
- Copy the first letter of the name and the first letter of the surname
- lblInitials shows the capitals with dots: T.M.
Hints
- The first initial is simply Copy(sFull, 1, 1) — or sFull[1].
- The surname starts one position after the space: Copy(sFull, iSpace + 1, 1).
- 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;