Count selected words in a string

I need help with this I want to count the number of selected words that are in a string.

lets say the string is..
'Provide a brief overview. This should be concise, but give enough information that an expert will get an idea of whether or not they have the expertise to answer your question.'

I would like to know how many times 'is' has been shown in this string above, there should be 3 instences of the 'is'

can anyone help?

Thanks
Sal.
LVL 2
SaLzAsked:
Who is Participating?
 
AlievConnect With a Mentor Commented:
There are two methods.
1st method is very simple:

function stcount(text:string;substring:string):integer;
var
i:integer; tmp_text:string;
begin
tmp_text:=text; i:=0;
       while pos(substring,tmp_text)<>0 do begin
               delete(tmp_text,pos(substring,tmp_text),length(substring));    // deletes substring in tmp_text
               inc(i);    // increases substring count(i)
       end;

stcount:=i;          //returns substring count in text
end;
0
 
mokuleConnect With a Mentor Commented:
The procedure below counts substring.
Do You want count 'mist' also or not?


procedure TForm1.Button5Click(Sender: TObject);
var
  P: integer;
  Count: integer;
  YourText: string;
  YourWord: string;
begin
  Count := 0;
  P := 0;
  repeat
    begin
    YourText := 'Provide a brief overview. This should be concise, but give enough information that an expert will get an idea of whether or not they have the expertise to answer your question';
    YourWord := 'is';
    P := PosEx(YourWord,YourText,P+1);
    if P > 0 then
      begin
// additional check here to test weather it is word
      Inc(Count);
      end;
    end;
  until P = 0;
//  Edit1.Text := IntToStr(Count);
end;
0
 
AlievCommented:
2nd method looks for counting matchcase words. If it`s interesting i can write it.
0
Cloud Class® Course: C++ 11 Fundamentals

This course will introduce you to C++ 11 and teach you about syntax fundamentals.

 
SaLzAuthor Commented:
like if I put in 'is' it will count how many 'is' are in the string, is I want to put 'mist' then it will count how many mist are in the string, is I put 'mister' is will count how many 'mister' there are and so on, like anything I put it will count it.
0
 
SaLzAuthor Commented:
ya plz, whats your MatchCASE words like, could you put that in here 2?
0
 
SaLzAuthor Commented:
mokule, when I use your source I get an error, Undeclared identifier: 'PosEx' are you using anything in your uses?

Aliev, your code works and you put it into a function which will keep it sort.
edit1.text:=inttostr(stcount(memo1.text,edit2.text));

thankyou, Aliev, this does the job well, plus it does work with CaSe which is another good point with your code.



0
 
mokuleCommented:
Sorry
uses
   StrUtils;
0
 
AlievCommented:
SaLz, my 2nd method searchs only words . F.e: text:='HEllo Experts. I schoolchildren from Azerbaijan and interesting about programming and aikido';

It will claculate words in text (12 words);
My first function wordc counts words on text
My second function returns word by index on text. And here is  other additional functions:

My code is written on free pascal. You can chenge it easly to delphi.


const
tex='HEllo Experts. I schoolchildren from Azerbaijan and interesting about programming and aikido';  // Nash tekst
delim=[' ','.',',','!','?'];   // znaki (rezdeliteli slov)
var
i:integer;

function word(index:integer):string;    // Returns index-th word from text
var
i:integer;
wrd:string;
wc:integer;
wr:boolean;txt:string;done:boolean;
begin
 wrd:='';wc:=0;wr:=false; txt:=' '+tex+' '; done:=false;
 for i:=1 to length(txt) do begin

   if not(txt[i] in delim) then begin
      wrd:=wrd+txt[i];
      if not(wr) then wr:=true;
   end
   else begin
      if wr then begin
      if wc=index then begin  word:=wrd; done:=true; end;
      inc(wc); wr:=false; wrd:=''; if done then break;
      end;
   end;

 end;
end;

function wordc():integer;    //returns word count
var
i:integer;
wrd:string;
wc:integer;
wr:boolean;txt:string;done:boolean;
begin
 wrd:='';wc:=0;wr:=false; txt:=' '+tex+' ';
 for i:=1 to length(txt) do begin

   if not(txt[i] in delim) then begin
      wrd:=wrd+txt[i];
      if not(wr) then wr:=true;
   end
   else begin
      if wr then begin
      inc(wc); wr:=false; wrd:='';
      end;
   end;

 end;
wordc:=wc;
end;

function longword():string;     //returns long word
var
i:integer;
wrd:string;
begin
wrd:='';
    for i:=0 to wordc()-1 do begin
     if length(wrd)<length(word(i)) then
      wrd:=word(i);
    end;
longword:=wrd;
end;

function curwordc(worrd:string):integer;     //  returns count worrd in tex
var
i:integer;
wrd:string;wc:integer;
begin
wrd:='';wc:=0;
    for i:=0 to wordc()-1 do begin
       if word(i)=worrd then inc(wc);
    end;
curwordc:=wc;
end;

function replace(s1,s2:string):string;    replaces s1 to s2 and returns replaced version of text
var
txt:string;i:integer;
begin
txt:=tex;
 while pos(s1,txt)<>0 do begin
    i:=pos(s1,txt);
    delete(txt,i,length(s1));
    insert(s2,txt,i);
 end;
replace:=txt;
end;

function polidr():integer;    // returns count of words like 'anna', 'dad', 'ata', ( abcdcba)
var
k,i:integer;wrd:string;pc:integer;wp1,wp2,wp3:string;
begin
pc:=0;wp1:='';wp2:='';wp3:='';
   for i:=1 to wordc do begin

     wrd:=word(i);
     if wrd[1]=wrd[length(wrd)] then begin
        wp1:=copy(wrd,1,length(wrd) div 2);
        wp2:=copy(wrd,length(wrd) -(length(wrd) div 2)+1,length(wrd) div 2)
     end;
     for k:=1 to length(wp2) do
         wp3:=wp3+wp2[length(wp2)-k+1];
     if (wp1<>'') and (wp1=wp3) then inc(pc);
   wp3:='';wp1:='';wp2:='';
   end;
polidr:=pc;
end;

begin
randomize;
writeln;writeln;
writeln(tex);
writeln;writeln;
writeln('Count of words:',wordc());
writeln('Random word:',word(random(wordc)));
writeln('Long word:',longword());
writeln('Count of Hello in text:',curwordc('Hello'));
writeln('Replaced text:',replace('Hello','Good bye'));
writeln('"Polidr" words count:',polidr());
end.



Best Regards,
Adil Aliev
Baku, Azerbaijan
ICQ: 174321977
0
 
SaLzAuthor Commented:
I've increased the points to 500. since both of your code work you both can have points.

mokule: 100pts, for his help.
Aliev: 400pts, for his help.

Thanksyou both.
Sal.
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.