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:
| Tool | Gives | Example |
|---|---|---|
| Length(s) | number of characters | Length('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 capitals | UpCase('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
- Read the text and the letter (use the first character of edtLetter: sLetter[1])
- Loop through the text comparing characters case-insensitively
- lblResult shows: Letter r appears 3 time(s)
Hints
- Initialise iCount := 0; before the loop.
- Compare with UpCase on both sides: UpCase(sText[i]) = UpCase(cLetter)
- 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;