Solved

Count selected words in a string

Posted on 2004-04-07
9
436 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
Independent Software Vendors: 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

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!

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Delphi - replicating a form 8 108
creating threads in delphi 1 189
shape, triangle, dbctrlgrid 3 42
Using MMsystem To change audio input 1 36
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…
This article explains how to create forms/units independent of other forms/units object names in a delphi project. Have you ever created a form for user input in a Delphi project and then had the need to have that same form in a other Delphi proj…
Finds all prime numbers in a range requested and places them in a public primes() array. I've demostrated a template size of 30 (2 * 3 * 5) but larger templates can be built such 210  (2 * 3 * 5 * 7) or 2310  (2 * 3 * 5 * 7 * 11). The larger templa…
Finding and deleting duplicate (picture) files can be a time consuming task. My wife and I, our three kids and their families all share one dilemma: Managing our pictures. Between desktops, laptops, phones, tablets, and cameras; over the last decade…

739 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