Grade 10 · Loops & first strings · Lesson and coding task · about 12 minutes
Open this task in the Delphi workspace →
Strings are just rows of characters — and you can visit them one character at a time. Two essentials:
Length(sName) // how many characters
sName[3] // the 3rd character (counting starts at 1!)
Combine them with a FOR loop and you can take any string apart:
for i := 1 to Length(sName) do
memOutput.Lines.Add(sName[i]); // one character per line
UpperCase(s) and LowerCase(s) convert a whole string. (Grade 11 goes much deeper — Copy, Pos, Delete and friends. This is the warm-up.)
Your task
Take the name in the edit box, SHOUT it into the memo, report its length on the label, then spell it vertically — one letter per line.
What you will do
- First memo line: the name in capitals with an ! after it, e.g. SIPHO!
- lblInfo shows: Your name has 5 letters
- Then one letter of the (original) name per memo line
Hints
- UpperCase(sName) + '!' makes the shout.
- Length gives an Integer — IntToStr it into the label text.
- for i := 1 to Length(sName) do memOutput.Lines.Add(sName[i]);
Model solution
procedure TfrmName.btnShoutClick(Sender: TObject);
var
sName : String;
i : Integer;
begin
sName := edtName.Text;
memOutput.Lines.Add(UpperCase(sName) + '!');
lblInfo.Caption := 'Your name has ' + IntToStr(Length(sName)) + ' letters';
for i := 1 to Length(sName) do
memOutput.Lines.Add(sName[i]);
end;