Для первой части (uses Character
),
function CountWordsInMemo(AMemo: TMemo): integer;
var
i: Integer;
IsWhite, IsWhiteOld: boolean;
txt: string;
begin
txt := AMemo.Text;
IsWhiteOld := true;
result := 0;
for i := 1 to length(txt) do
begin
IsWhite := IsWhiteSpace(txt[i]);
if IsWhiteOld and not IsWhite then
inc(result);
IsWhiteOld := IsWhite;
end;
end;
Для второй части,
function OccurrencesOfWordInMemo(AMemo: TMemo; const AWord: string): integer;
var
LastPos: integer;
len: integer;
txt: string;
begin
txt := AMemo.Text;
result := 0;
LastPos := 0;
len := Length(AWord);
repeat
LastPos := PosEx(AWord, txt, LastPos + 1);
if (LastPos > 0) and
((LastPos = 1) or not IsLetter(txt[LastPos-1])) and
((LastPos + len - 1 = length(txt)) or not IsLetter(txt[LastPos+len])) then
inc(result);
until LastPos = 0;
end;
function DensityOfWordInMemo(AMemo: TMemo; const AWord: string): real;
begin
result := OccurrencesOfWordInMemo(AMemo, AWord) / CountWordsInMemo(AMemo);
end;