Languages :: Pascal :: Help with count & strings |
|||
| By: VB guy |
Date: 26/03/2003 00:00:00 |
Points: 40 | Status: Answered Quality : Excellent |
|
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 = word Then C := C+1; Writeln('The word " '+word+' " In phrase " '+phrase+' " ',C,' Times.'); Readln; End. PLZ HELP |
|||
| By: VGR | Date: 26/03/2003 08:10:00 | Type : Comment |
|
| duplicate |
|||
| By: VGR | Date: 26/03/2003 08:11:00 | Type : Answer |
|
| 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=' ') 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 ;-) |
|||
| By: VB guy | Date: 26/03/2003 08:52:00 | Type : Comment |
|
| 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. |
|||
| By: VGR | Date: 26/03/2003 11:38:00 | Type : Comment |
|
| <A HREF="http://www.experts-exchange.com/Programming/Programming_Languages/Pascal/Q_20563682.html">http://www.experts-exchange.com/Programming/Programming_Languages/Pascal/Q_20563682.html</a> |
|||
| By: MannSoft | Date: 27/03/2003 00:49:00 | Type : Comment |
|
| 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. |
|||
| By: collegeBoy | Date: 27/03/2003 05:48:00 | Type : Comment |
|
| 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 |
|||
| By: collegeBoy | Date: 27/03/2003 05:50:00 | Type : Comment |
|
| VGR Don´t you think that separate in words is more difficult??? Consider a comma and a period like separators and numbers and substrings, ... |
|||
| By: MannSoft | Date: 27/03/2003 05:59:00 | Type : Comment |
|
| 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) 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. |
|||
| By: VGR | Date: 27/03/2003 06:14:00 | Type : Comment |
|
| 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. |
|||
| By: collegeBoy | Date: 27/03/2003 06:39:00 | Type : Comment |
|
| Maybe it is more logical but also complex |
|||
| By: MannSoft | Date: 27/03/2003 06:41:00 | Type : Assist |
|
| 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; |
|||
| By: VGR | Date: 27/03/2003 06:54:00 | Type : Comment |
|
| 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 |
|||
| By: MannSoft | Date: 27/03/2003 19:13:00 | Type : Comment |
|
| 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. |
|||
| By: VGR | Date: 27/03/2003 19:54:00 | Type : Comment |
|
| right, I didn't see the SPC here on EE (too narrow on screen ;-) it's ***a*** solution. |
|||
|
Do register to be able to answer |
|||
©2010 These pages are served without commercial sponsorship. (No popup ads, etc...). Bandwidth abuse increases hosting cost forcing sponsorship or shutdown. This server aggressively defends against automated copying for any reason including offline viewing, duplication, etc... Please respect this requirement and DO NOT RIP THIS SITE.
Please DO link to this page!








