Solved

Count selected words in a string

Posted on 2004-04-07
9
437 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
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

Announcing the Most Valuable Experts of 2016

MVEs are more concerned with the satisfaction of those they help than with the considerable points they can earn. They are the types of people you feel privileged to call colleagues. Join us in honoring this amazing group of Experts.

Question has a verified solution.

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

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…
Introduction Raise your hands if you were as upset with FireMonkey as I was when I discovered that there was no TListview.  I use TListView in almost all of my applications I've written, and I was not going to compromise by resorting to TStringGrid…
This video Micro Tutorial shows how to password-protect PDF files with free software. Many software products can do this, such as Adobe Acrobat (but not Adobe Reader), Nuance PaperPort, and Nuance Power PDF, but they are not free products. This vide…
This is my first video review of Microsoft Bookings, I will be doing a part two with a bit more information, but wanted to get this out to you folks.

729 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