?
Solved

Pascal assignment , need help :(

Posted on 2003-03-26
4
Medium Priority
?
333 Views
Last Modified: 2010-04-16
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[i] = word then c := c+1;
writeln;
writeln('Found word "'+word+'" in phrase "'+phrase+'" ',c,' times.');
readln;
end.

Please help
0
Comment
Question by:The_ARA
  • 2
  • 2
4 Comments
 
LVL 15

Accepted Solution

by:
VGR earned 300 total points
ID: 8210478
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.
0
 
LVL 15

Expert Comment

by:VGR
ID: 8210490
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
 
LVL 1

Expert Comment

by:Okey
ID: 8248258
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.
0
 
LVL 1

Expert Comment

by:Okey
ID: 8248269
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.
0

Featured Post

Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

Question has a verified solution.

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

This blog will spread awareness about Dropbox. We have given the statements based upon our experience. Along with this, there is a section of some new plans that should be added in Dropbox this year. This will make the storage service enhanced from …
Article by: Tammy
MySQLTuner is a script written in Perl that allows you to review a MySQL installation quickly and make adjustments to increase performance and stability. The current configuration variables and status data is retrieved and presented in a brief forma…
Integration Management Part 2
Whether it be Exchange Server Crash Issues, Dirty Shutdown Errors or Failed to mount error, Stellar Phoenix Mailbox Exchange Recovery has always got your back. With the help of its easy to understand user interface and 3 simple steps recovery proced…
Suggested Courses

621 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