visitor (0 QPoints)
  • FR
  • EN
  • NL
  • DE
  • ES
315 experts, 1193 registered users, 1659 questions already answered
European Experts Exchange, the very best site for high-quality IT solutions

New Improved Search!

 


05/10/2011 1h30 : Steve Jobs is dead, the father of Apple ][ is gone, we are all orphaned.

Languages :: Pascal :: Pascal assignment , need help :(


By: collegeBoy U.S.A.  Date: 26/03/2003 00:00:00  English  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 English  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 English  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 English  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

EContact
browser fav
page generated in 339.635130 milliseconds

Why Google AdSense ads ?

compteur
 Ranking-Hits PageRank for this page