Solved

Count selected words in a string

Posted on 2004-04-07
9
433 Views
Last Modified: 2012-05-04
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.
0
Comment
Question by:SaLz
  • 4
  • 3
  • 2
9 Comments
 
LVL 17

Assisted Solution

by:mokule
mokule earned 100 total points
ID: 10776184
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
 
LVL 3

Accepted Solution

by:
Aliev earned 400 total points
ID: 10776187
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
 
LVL 3

Expert Comment

by:Aliev
ID: 10776218
2nd method looks for counting matchcase words. If it`s interesting i can write it.
0
Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

 
LVL 2

Author Comment

by:SaLz
ID: 10776277
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
 
LVL 2

Author Comment

by:SaLz
ID: 10776386
ya plz, whats your MatchCASE words like, could you put that in here 2?
0
 
LVL 2

Author Comment

by:SaLz
ID: 10776474
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
 
LVL 17

Expert Comment

by:mokule
ID: 10776498
Sorry
uses
   StrUtils;
0
 
LVL 3

Expert Comment

by:Aliev
ID: 10776576
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
 
LVL 2

Author Comment

by:SaLz
ID: 10776656
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

Featured Post

Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
Using idhttp to login to instagram 2 106
QRReport  TQrmemo vertical stretching 1 53
Delphi: sending SMS on android platform 1 34
MS Access from Delphi 31 60
The uses clause is one of those things that just tends to grow and grow. Most of the time this is in the main form, as it's from this form that all others are called. If you have a big application (including many forms), the uses clause in the in…
Introduction I have seen many questions in this Delphi topic area where queries in threads are needed or suggested. I know bumped into a similar need. This article will address some of the concepts when dealing with a multithreaded delphi database…
This video shows how to quickly and easily add an email signature for all users on Exchange 2016. The resulting signature is applied on a server level by Exchange Online. The email signature template has been downloaded from: www.mail-signatures…

860 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question