Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium

x
Solved

# Help with count & strings

Posted on 2003-03-26
Medium Priority
224 Views
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
0
Question by:sajidaziz
• 7
• 4
• 3
• +2

LVL 15

Expert Comment

ID: 8213324
duplicate
0

LVL 15

Accepted Solution

VGR earned 160 total points
ID: 8213335
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

Author Comment

ID: 8213633
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

LVL 15

Expert Comment

ID: 8214610
0

LVL 6

Expert Comment

ID: 8217751
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

Expert Comment

ID: 8219769
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

Expert Comment

ID: 8219783
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

LVL 6

Expert Comment

ID: 8219830
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

LVL 15

Expert Comment

ID: 8219931
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

Expert Comment

ID: 8220085
Maybe it is more logical but also complex
0

LVL 6

Expert Comment

ID: 8220102
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

LVL 15

Expert Comment

ID: 8220179

Destructive (I don't like this) but efficient

I think this takes "ball" out of "baseball", while my way doesn't
0

LVL 6

Expert Comment

ID: 8220288
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

LVL 15

Expert Comment

ID: 8220479
right, I didn't see the SPC here on EE (too narrow on screen ;-)

it's ***a*** solution.
0

Expert Comment

ID: 9314354
sajidaziz:
EXPERTS:
Post your closing recommendations!  No comment means you don't care.
0

LVL 15

Expert Comment

ID: 9324080
easy points to me :D
0

## Featured Post

Question has a verified solution.

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

How does storytelling impact the web? What goes into a great story on the web? And how can it be used for marketing purposes? All of these questions will be answered.
Iâ€™m willing to make a bet that your organization stores sensitive data in your Windows File Servers; files and folders that you really donâ€™t want making it into the wrong hands.