?
Solved

Pascal assignment , need help :(

Posted on 2003-03-26
4
Medium Priority
?
324 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
[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
  • 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

New feature and membership benefit!

New feature! Upgrade and increase expert visibility of your issues with Priority Questions.

Question has a verified solution.

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

The top devops trends for 2017 are focused on improved deployment frequency, decreased lead time for change and decreased MTTR.
Ready to get certified? Check out some courses that help you prepare for third-party exams.
If you’ve ever visited a web page and noticed a cool font that you really liked the look of, but couldn’t figure out which font it was so that you could use it for your own work, then this video is for you! In this Micro Tutorial, you'll learn yo…
In this video, Percona Director of Solution Engineering Jon Tobin discusses the function and features of Percona Server for MongoDB. How Percona can help Percona can help you determine if Percona Server for MongoDB is the right solution for …
Suggested Courses

764 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