?
Solved

Help with count & strings

Posted on 2003-03-26
17
Medium Priority
?
223 Views
Last Modified: 2010-04-16
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
0
Comment
Question by:sajidaziz
[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
  • 7
  • 4
  • 3
  • +2
17 Comments
 
LVL 15

Expert Comment

by:VGR
ID: 8213324
duplicate
0
 
LVL 15

Accepted Solution

by:
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.');
 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
 

Author Comment

by:sajidaziz
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
Enroll in August's Course of the Month

August's CompTIA IT Fundamentals course includes 19 hours of basic computer principle modules and prepares you for the certification exam. It's free for Premium Members, Team Accounts, and Qualified Experts!

 
LVL 6

Expert Comment

by:MannSoft
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

by:cang24
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

by:cang24
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

by:MannSoft
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

by:VGR
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

by:cang24
ID: 8220085
Maybe it is more logical but also complex
0
 
LVL 6

Expert Comment

by:MannSoft
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

by:VGR
ID: 8220179
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
 
LVL 6

Expert Comment

by:MannSoft
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

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

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

Expert Comment

by:CleanupPing
ID: 9314354
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
 
LVL 15

Expert Comment

by:VGR
ID: 9324080
easy points to me :D
0

Featured Post

Want to be a Web Developer? Get Certified Today!

Enroll in the Certified Web Development Professional course package to learn HTML, Javascript, and PHP. Build a solid foundation to work toward your dream job!

Question has a verified solution.

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

We are witnesses that everyone is saying that our children shouldn't "play" with a technology because it is dangerous. This article is going to prove that they are wrong.
Active Directory can easily get cluttered with unused service, user and computer accounts. In this article, I will show you the way I like to implement ADCleanup..
Do you want to know how to make a graph with Microsoft Access? First, create a query with the data for the chart. Then make a blank form and add a chart control. This video also shows how to change what data is displayed on the graph as well as form…
Sometimes it takes a new vantage point, apart from our everyday security practices, to truly see our Active Directory (AD) vulnerabilities. We get used to implementing the same techniques and checking the same areas for a breach. This pattern can re…
Suggested Courses
Course of the Month13 days, 19 hours left to enroll

801 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