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 : ');
Write('Enter a 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.');
End.

PLZ HELP
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.

Commented:
duplicate
0
Commented:
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.');
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

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

Author 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
Commented:
0
Commented:
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
Commented:
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
Commented:
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
Commented:
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
Commented:
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
Commented:
Maybe it is more logical but also complex
0
Commented:
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
Commented:

Destructive (I don't like this) but efficient

I think this takes "ball" out of "baseball", while my way doesn't
0
Commented:
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
Commented:
right, I didn't see the SPC here on EE (too narrow on screen ;-)

it's ***a*** solution.
0
Commented:
sajidaziz:
EXPERTS:
Post your closing recommendations!  No comment means you don't care.
0
Commented:
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.