Help with count & strings

Program INPHRASE;
I am writing a program that counts the amount of times a word appears in phrase.
Can anyone help me with this program, It runs but does not count the amount of times a word appears in a phrase.


Uses Crt;
Var phrase, word : String;
 C, I: Integer;
Begin
Clrscr;
Write('Enter a phrase : ');
Readln(phrase);
Write('Enter a word :');
Readln(word);
C := 0;
For I := 1 To Length (phrase) Do
If phrase [I] = word Then C := C+1;
Writeln('The word " '+word+' " In phrase " '+phrase+' " ',C,' Times.');
Readln;
End.

PLZ HELP
sajidazizAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

VGRCommented:
duplicate
0
VGRCommented:
are you two in the same classroom ??? ;-)

no

do it like this

1) separate the phrase in words using space as a separator (in a loop)
2) each time you found a word, compare it to the searched one and count
3) loop

Something like this (not compiled nor tested against typos)

program INPHRASE;
uses crt;
var word : string;
   phrase : string;
   i,j : integer;
   c : integer;
   limit : word;

begin
 clrscr;
 write('Enter a phrase : '); readln(phrase);
 writeln;
 write('Enter a word : '); readln(word);
 c :=0;
 i:=0;
 limit:=Length(phrase);
 Repeat
   (* get a word *)
   j:=i;
   repeat inc(i); until (phrase[i]=' ') or (i=limit);
   (* compare to word and count *)
   if Copy(phrase,j,i-j+1) = word then c := c+1;
 Until i=limit;
 (* display *)
 writeln;
 writeln('Found word "'+word+'" in phrase "'+phrase+'" ',c,' times.');
 readln;
end.  


Comment from VGR  03/26/2003 06:22AM PST  
it's lacking to skip the space found in the loop, and perhaps failing to treat the last word, but the general idea is this.

I suppose that for a "homework help" it's enough to provide almost-good solutions ;-)  
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
sajidazizAuthor Commented:
Same class??? what's that all about???

I've tried changing things to make that program work, but it's not having any of it.
0
Cloud Class® Course: Amazon Web Services - Basic

Are you thinking about creating an Amazon Web Services account for your business? Not sure where to start? In this course you’ll get an overview of the history of AWS and take a tour of their user interface.

MannSoftCommented:
It must be a chunk of code the teacher gave the students and said "fix it".  I answered this question on a different messageboard yesterday as well.
0
cang24Commented:
Whatever

You must check word=word (ok is character=character in a cycle)
not word=character

The cycle starts when you found the first caracter.

ooh and don't forget this case

word=carmagedon
phrase=carcarmagedon

don´t loose the initial position that you are checking, I mean the first character you found

0
cang24Commented:
VGR

Don´t you think that separate in words is more difficult???
Consider a comma and a period like separators and numbers and substrings, ...
0
MannSoftCommented:
Yeah my answer didnt break it into words either, it just searched for substrings.  I just changed two of the lines leaving the rest of the code intact.

To quote the answer I gave:

You are comparing only one character in phrase (phrase[I]) against the whole word, which is where your problem is. Try this:

for I := 1 to Length(Phrase) - Length(Word) + 1 do
  if (Copy(Phrase, I, Length(Word)) = Word) then
    Inc(C);

Do a paper test with a couple short phrases and words and Im sure you'll see how it works.
0
VGRCommented:
no more difficult.

He wants to count occurence of words, so splitting the stuff to analyse into words seems the MOST LOGICAL to me ;-)

It's true that some characters (punctuation) are to be ignored, but it doesn't change anything to the principla :
-split into words
-clean each word of punctuation characters (. , ; :) in case they were. You may also "clean" the whole stuff before splitting into words
-count occurence of the searched word in the set of words found.

I don't see any other solution.
0
cang24Commented:
Maybe it is more logical but also complex
0
MannSoftCommented:
Well it depends on if he wants whole word or substring matchine.  IE:

phrase = "I like baseball"
word = "ball"

Depending on what kind of matching he wants, that may or may not return a match.  But in either case, I think the shortest code would be to do:

//For whole-word matching, uncomment these lines
//StripPunctuation is of course a custom function
//Phrase := ' ' + StripPunctuation(Phrase) + ' ';
//Word := ' ' + Trim(Word) + ' ';

Count := 0;
while Pos(Word, Phrase) > 0 do
begin
  Inc(Count);
  Delete(Phrase, 1, Pos(Word, Phrase) + Length(Word));
end;
0
VGRCommented:
not bad at all.

Destructive (I don't like this) but efficient

I think this takes "ball" out of "baseball", while my way doesn't
0
MannSoftCommented:
Yes it's destructive, but personally I would put it into a function (since it's general purpose and has the possibility for re-use), so that wouldnt matter.

And if you uncomment the 2 lines above it, it would not take the "ball" out of baseball" since the lines wrap the search Word in spaces which effectively forces whole word matches.  It also wraps the Phrase with spaces so the first and last word can be properly matched.
0
VGRCommented:
right, I didn't see the SPC here on EE (too narrow on screen ;-)

it's ***a*** solution.
0
CleanupPingCommented:
sajidaziz:
This old question needs to be finalized -- accept an answer, split points, or get a refund.  For information on your options, please click here-> http:/help/closing.jsp#1 
EXPERTS:
Post your closing recommendations!  No comment means you don't care.
0
VGRCommented:
easy points to me :D
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Pascal

From novice to tech pro — start learning today.

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.