Solved

Count selected words in a string

Posted on 2004-04-07
9
430 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
 
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
Highfive + Dolby Voice = No More Audio Complaints!

Poor audio quality is one of the top reasons people don’t use video conferencing. Get the crispest, clearest audio powered by Dolby Voice in every meeting. Highfive and Dolby Voice deliver the best video conferencing and audio experience for every meeting and every room.

 
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

Maximize Your Threat Intelligence Reporting

Reporting is one of the most important and least talked about aspects of a world-class threat intelligence program. Here’s how to do it right.

Join & Write a Comment

A lot of questions regard threads in Delphi.   One of the more specific questions is how to show progress of the thread.   Updating a progressbar from inside a thread is a mistake. A solution to this would be to send a synchronized message to the…
Objective: - This article will help user in how to convert their numeric value become words. How to use 1. You can copy this code in your Unit as function 2. than you can perform your function by type this code The Code   (CODE) The Im…
This tutorial demonstrates a quick way of adding group price to multiple Magento products.
This video explains how to create simple products associated to Magento configurable product and offers fast way of their generation with Store Manager for Magento tool.

760 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

Need Help in Real-Time?

Connect with top rated Experts

19 Experts available now in Live!

Get 1:1 Help Now