Languages :: Pascal :: Pascal assignment , need help :( |
|||
| By: collegeBoy |
Date: 26/03/2003 00:00:00 |
Points: 75 | Status: Answered Quality : Excellent |
|
Write a program that; Prompts and permits the user to enter a phrase. Prompts and permits the user to enter a word. Reveals the number of times that the word occurs in the phrase. (N.B. Words within words do not count). ive tried so far program INPHRASE; uses crt; var word : string; phrase : string; i : integer; c : integer; begin clrscr; write('Enter a phrase : '); readln(phrase); writeln; write('Enter a word : '); readln(word); c :=0; for i := 1 to length(phrase) Do if phrase = word then c := c+1; writeln; writeln('Found word "'+word+'" in phrase "'+phrase+'" ',c,' times.'); readln; end. Please help |
|||
| By: VGR | Date: 26/03/2003 01:20:00 | Type : Answer |
|
| 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. |
|||
| By: VGR | Date: 26/03/2003 01:22:00 | Type : Comment |
|
| 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: TheFalklands | Date: 01/04/2003 19:01:00 | Type : Comment |
|
| Maybe you're able to scan for the Word expression in a string with POS. Then copy the Rest of the String Now Repat Expression scan and rest-copy process till the end of String. Do this like you want! I'm sure you're able to. |
|||
|
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!








