var
s:string;
count:integer;
begin
readln(s);
while pos('ab',s) > 0 do begin
inc(count);
delete(s,pos('ab',s),2);
end;
writeln(count);
end.
Вариант 2
function CountSym(ch,s: string): byte;
begin
if Pos(ch, s) <> 0 then
CountSym := 1 + CountSym(ch, Copy(s, Pos(ch,s)+1, Length(s)))
else
CountSym := 0
end;