LearnStanmorephysics

Grade 11 · Powering Up

Searching inside strings

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

Open this task in the Delphi workspace →

Grade 11 is the year of the string. Your scalpel set:

ToolGivesExample
Length(s)number of charactersLength('Karoo') = 5
s[i]character number i'Karoo'[2] = 'a'
Pos(sub, s)where sub starts (0 = not found)Pos('roo', 'Karoo') = 3
UpCase(c)one Char in capitalsUpCase('r') = 'R'

The bread-and-butter pattern — walk the string and count something:

iCount := 0;
for i := 1 to Length(sText) do
  if UpCase(sText[i]) = UpCase(cLetter) then
    Inc(iCount);

Using UpCase on both sides makes the comparison case-insensitive — 'r' matches 'R'.

Your task

Count how many times a letter appears in a piece of text, ignoring case.

What you will do

Hints
  1. Initialise iCount := 0; before the loop.
  2. Compare with UpCase on both sides: UpCase(sText[i]) = UpCase(cLetter)
  3. The caption joins the Char and the count: 'Letter ' + cLetter + ' appears ' + IntToStr(iCount) + ' time(s)'
Model solution
procedure TfrmCount.btnCountClick(Sender: TObject);
var
  sText : String;
  cLetter : Char;
  i, iCount : Integer;
begin
  sText := edtText.Text;
  cLetter := edtLetter.Text[1];
  iCount := 0;
  for i := 1 to Length(sText) do
    if UpCase(sText[i]) = UpCase(cLetter) then
      Inc(iCount);
  lblResult.Caption := 'Letter ' + cLetter + ' appears ' + IntToStr(iCount) + ' time(s)';
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.