Solved

Pascal Wordcount program

Posted on 1998-12-15
15
334 Views
Last Modified: 2010-05-18
I need a Pascal wordcount program, has anyone got any sources? If so could you please email them to me ASAP.
Many thanks in advance
Ozi
0
Comment
Question by:Ozi
  • 6
  • 3
  • 2
  • +3
15 Comments
 
LVL 2

Expert Comment

by:omsec
ID: 1216522
how do you mean that ?
do you mean a program that takes a file name as input (text file) and counts the words in it or counting words in "real time" when they are typed directly in that program ?
0
 

Author Comment

by:Ozi
ID: 1216523
Thanks for your reply, let me be more specific. The program asks you for the name of a text file. It then reads this file and returns all the words that are in this file along with the line numbers they appear on in the form of another text file.

Can you help at all omsec?
0
 
LVL 7

Expert Comment

by:Inteqam
ID: 1216524
you need nearly a lexical.

0
 

Author Comment

by:Ozi
ID: 1216525
a what Integam?? I don't understand, i know very little pascal and i need this wordcount program in pascal. Please can anyone help me?

Many thanks
Ozi
0
 
LVL 7

Expert Comment

by:Inteqam
ID: 1216526
a lexical is a part of a compiler .
just kidding ,

give me until tomorrow.

0
 

Author Comment

by:Ozi
ID: 1216527
thank you very much inteqam, would it be possible by 12:00 GMT?

Thank you again
Ozi
0
 

Author Comment

by:Ozi
ID: 1216528
omsec, have you got any new ideas for my program?

thanks

Ozi
0
How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

 
LVL 3

Expert Comment

by:traygreen
ID: 1216529
Basically all you need to do is loop through the file one character at a time looking for your delimiting characters.  Append each of the characters to a temporary string.
When a delimiting character is reached,
   Store and clear the temporary string
   get the next character

(Obviously you will need some sort of structure in which to store the words and their line numbers/count. oring Depending on the size of the file and or word list, you may want to use another file rather than storing them in memory.)

What characters are you using to delimit a "word"?
space

,
!
?
(
)
ect.

You wouldn't happen to need this for a class would you?

0
 

Author Comment

by:Ozi
ID: 1216530
yes i do. I'll send you my code so far, i've used delimeters and stuff. But i need to store each word in a list and then each time a word is added to a list i have to check through the list to see if it occured previously. If it has occured then i have to somehow store the line number for that word and go on to next word until the inputfile is read. Can you please help me?

All i've managed to do so far is to read each word and write it to new text file. Each word is on a seperate line. I need to somehow transfer each word to a list/array. That is the part i can't do. I need some help desperately. Can you help me implement it please?

Here's my code so far...



Program WordCount (input, output) ;

Type CharSet  = SET of Char ;


Var InputFile, OutputFile : Text ;
    EndOfWordOrSentenceSymbols, LetterSet : CharSet ;


Function ExistFile (FileName : String) : Boolean ;
Var TextFile : Text ;

Begin
     Assign (Textfile, FileName) ;
     Reset (TextFile) ;
     IF IOResult = 0 Then
        Begin
             Close (TextFile) ;
             ExistFile := True
        End
     Else
         ExistFile := False
End ;


Procedure OpenInputFile (Var InputFile : Text) ;
Var FileName : String ;
Begin
     Write ('Enter name of input file: ') ;
     Readln (FileName) ;
     While not ExistFile (FileName) Do
           Begin
                Writeln ('Can not find file ', FileName) ;
                Write ('Enter name of input file: ') ;
                Readln (FileName)
           End ;
Assign (InputFile, FileName) ;
Reset (InputFile)
End ;


Procedure OpenOutputFile (Var OutPutFile : Text) ;
Var FileName : String ;

Begin
     Write ('Enter a filename you can remember! : ') ;
     Readln (FileName) ;
     Assign (OutputFile, FileName) ;
     Rewrite (OutputFile)
End ;



Procedure ReadWords (Var InputFile, OutputFile : Text) ;
Var Character : Char ;
    NextLine  : String ;
Begin
EndOfWordOrSentenceSymbols := ['.', '(', ')', '!', '?', ':', ',', ';', ' ', '/', '^'] ;
LetterSet := ['a' .. 'z' , 'A' ..'Z'] ;
     While Not EoF (InputFile) Do
           Begin
           While Not Eoln (InputFile) Do
                Begin
                Read (InputFile, Character) ;
                If Character In LetterSet Then
                        write (OutputFile, Character)
                Else
                If Character In EndOfWordOrSentenceSymbols Then
                        writeln (OutputFile)
                End ;
           Readln (InputFile, NextLine) ;  {<=== This line is neccessary}
           End ;                           {to skip to the next line of}
End ;                                      {text in the input file}


Begin
     OpenInputFile (InputFile) ;
     OpenOutputFile (OutputFile) ;
     ReadWords (InputFile, OutputFile) ;
     Close (InputFile) ;
     Close (OutputFile) ;
     readln ;
End.


many thanks for reading my emails and answering my cry for help.

it is much appreciated.

Many thanks, look forward to hearing from you.

Ozi



0
 

Author Comment

by:Ozi
ID: 1216531
can someone please help me?
0
 
LVL 5

Accepted Solution

by:
scrapdog earned 120 total points
ID: 1216532
type
  tline_node_ptr = ^tline_node;
  tline_node = record
                 linenum :longint;
                 next :tline_node_ptr;
               end;
  tnode_ptr = ^tnode;
  tnode = record
            word :string;
            count :integer;
            lines :tline_node_ptr;
            next :tnode_ptr;
          end;



procedure addlinenum(var nodeptr :tnode_ptr;  line :longint);
var
  cptr, newnode, prev :tline_node_ptr;
  found :boolean;
begin
  cptr := nodeptr^.lines;
  if cptr<>nil then begin
    while (cptr^.next <> nil) and (not found) do
      if cptr^.linenum = line then found := true
      else prev := cptr; cptr := cptr^.next;
    if not found then begin
      new(newnode);
      newnode^.next := nil;
      newnode^.linenum := line;
      prev^.next := newnode;
    end;
  end
  else begin
    new(cptr);
    cptr^.next := nil;
    cptr^.linenum := line;
    nodeptr^.lines := cptr;
  end;
  inc(nodeptr^.count);
end;




procedure add_word(var head_ptr :tnode_ptr; newword :string; line :longint);
var
  cptr, prev, newnode  :tnode_ptr;   found :boolean;
begin
  cptr := head_ptr;
  if cptr <> nil then begin
    prev := head_ptr;
    found := false;
    while (cptr^.next <> nil) and (not found) do
      if cptr^.word = newword then found := true
      else prev := cptr;  cptr := cptr^.next;
    if found then addlinenum(cptr, line)
    else begin
      new(newnode);
      newnode^.next := nil;
      newnode^.word := newword;
      newnode^.lines := nil;
      newnode^.count := 0;
      prev^.next := newnode;
      addlinenum(newnode, line);
    end;
  end
  else begin
    new(head_ptr);
    head_ptr^.next := nil;
    head_ptr^.word := newword;
    head_ptr^.lines := nil;
    head_ptr^.count := 0;
    addlinenum(head_ptr, line);
  end;
end;



procedure read_words(var infile :text;  var head_ptr :tnode_ptr;
                     var count :longint);

var line :longint;
    charin :char;
    wordflag :boolean;
    current_word :string;
begin
  wordflag := false;
  count := 0;
  line := 1;
  current_word := '';
  while not eof(infile) do begin
    read(infile,charin);
    charin := upcase(charin);
    if charin in ['A'..'Z'] then begin
      if not wordflag then begin inc(count); wordflag := true; end;
      current_word := current_word + charin;
    end
    else if wordflag then begin
        wordflag := false;
        add_word(head_ptr, current_word, line);
        current_word := '';
    end;
    if eoln(infile) then inc(line);
  end;
end;








procedure writereportfile(var outfile :text; var head_ptr :tnode_ptr);
var
  cptr, tempc :tnode_ptr;
  lptr, templ :tline_node_ptr;
begin
  cptr := head_ptr;
  while (cptr<>nil) do begin
    write(outfile, cptr^.word,'  (',cptr^.count,'):  ');
    lptr := cptr^.lines;
    while (lptr<>nil) do begin
      write(outfile, lptr^.linenum);
      templ := lptr^.next;
      if lptr<> nil then dispose(lptr);
      lptr := templ;
      if lptr<>nil then write(outfile, ', ');
    end;
    writeln(outfile);
    tempc := cptr^.next;
    if cptr<>nil then dispose(cptr);
    cptr := tempc;
  end;
  writeln(outfile);
end;

var
  head_ptr :tnode_ptr;

begin
  open in file here...

  head_ptr := nil;

  readwords;

  close in file here;

  open out file here;

  send head_ptr to writereportfile;

  close out file here;
end.


------------------------------
NOTE:  This is not intended to be a working program!!  It is a framework upon which you can base the remainder of your program on.  This will give you a start.  You will have to incorporate these functions with the existing functions you have (such as opening files, etc.).  

These function use a linked list of linked lists.  Head_ptr points to the head of the main linked list (a list of words), and the functions take care of the linked lists within the list (these are lists of line numbers).

You will have to do some tweaking.  Since this is for homework, I am going to leave the rest up to you.  Try to figure out how it works.
0
 
LVL 1

Expert Comment

by:quanghoc
ID: 1216533
 What do you transfer each words to a list for ? If you want, you can put each words into a array, don't save it on file.
0
 
LVL 5

Expert Comment

by:scrapdog
ID: 1216534
Pascal doesn't support dynamic arrays.
0
 
LVL 3

Expert Comment

by:traygreen
ID: 1216535
scapdog,
Use a linked list
0
 
LVL 5

Expert Comment

by:scrapdog
ID: 1216536
That's what I did!!
0

Featured Post

Threat Intelligence Starter Resources

Integrating threat intelligence can be challenging, and not all companies are ready. These resources can help you build awareness and prepare for defense.

Join & Write a Comment

A safe way to clean winsxs folder from your windows server 2008 R2 editions
Owning a franchise can be the dream of a lifetime. It provides a chance for economic growth. You can be as successful as you want.  To make your franchise successful, you need to market it successfully. Here are six of the best marketing strategies …
Polish reports in Access so they look terrific. Take yourself to another level. Equations, Back Color, Alternate Back Color. Write easy VBA Code. Tighten space to use less pages. Launch report from a menu, considering criteria only when it is filled…
You have products, that come in variants and want to set different prices for them? Watch this micro tutorial that describes how to configure prices for Magento super attributes. Assigning simple products to configurable: We assigned simple products…

759 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

Need Help in Real-Time?

Connect with top rated Experts

21 Experts available now in Live!

Get 1:1 Help Now