Search from a user created text file : 500 points

Hey experts i really need help on this. I just dont have any idea how to create a search engine for a text file. There is alot of restrictions for this program like you are not allowed to use the CRT command or use any strings. Can i see how the program and especially the text file that need to be search is set out? A program with an example of the text file is most appreciated. thx!

What to do
Write a program that searches for words in a text file. Read the text file of characters into an array and then prompt the user for a word to find. If the word is found then the program displays the lines containing the word and the line numbers. In addition to the basic search capability, provide in your program two options. The first should allow the user to request a search where the case of the letters in not significant. The second should allow the user to specify a whole word search, i.e. the word is not part of a larger word.


Data Specifications
words will not span lines
any blank lines will not be at the end of the file
Program Specifications
The Pascal program

must use arrays to store the characters from the text file

may NOT use the predefined string data type, nor records
can assume a maximum line length of 72 characters and a maximum of 50 lines
assumes that a word is a sequence of non-blank characters
must find successfully whole words that are at the beginning of a line, at the end of a line, or are preceded or succeeded by punctuation characters like periods, commas, etc.
please prompt the user for the text file name
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

floezemAuthor Commented:
By the way i need answers real soon, by thursday and no later if possible thx....i am really stuck on this
Turning in someone else's work as your own is _academic dishonesty_. Asking for someone to do your homework is against the membership agreement of this site as is my writing an answer for your assignment.

That said, it looks like you have a lot of work to do between now and Thursday.

I would suggest that you start by defining the type you will use to hold the contents of the text file. One thing to look at is writing the type and a procedure to read the contents of a named file into that data structre (I will bet if you look in your notes you can find how to declare an appropriate structure). Write a second procedure that prints out the contents of the array of characters (oh, my, did I give something away there?); this second procedure is not really necessary for the ASSIGNMENT but you will want to know that your read routine is working.

While you're workng on that, think about how you could tell whether an array of characters contained a particular substring. Imagine you had a deck of cards (face up) and a smaller pile of cards. How could you tell if the sequence found in the smaller pile of cards could be found in the deck of cards? I would ignore the special features until you have the simple match working. Once you write the simple match the ignore case match is really quite simple (ask some questions about it if you want); the other is a touch harder but only because of edge conditions.

Good luck and ask lots of specific questions, showing us your progress as you go.

floezemAuthor Commented:
lol seriously you dont have to accuse me of cheating, if i really want to cheat i have a much easier way of doing so, believe me. All i want is to have is a little example i can refer to. Besides this assignment is not just about searching with arrays, i hope you realized i merely copied and pasted a small segment of the assignment i was suppose to do. I do admit its the core of my assignment however and i have no idea how to go about it. I am merely being honest and i have been working long hours to try to solve it. I just want to learn how it is done and i need a clear example to help me out. Examples like how the text file that needed to be search is to be set up.
If i known i would be accused like this by asking how to solve a single problem i wouldn't have asked. I can just copy the search problem off my friend instead and still learn that way but i dont wanna. Still i am asking for a skeletal program with the text file example which work when i search it. I am not asking for the assignment handed to me on a platter. HElp if you will, or continue putting me down, you prolly dont know the effort i put into it because you think everyone is a genious at programming like you. I am just a learner and theres still a heck lot i dont know about pascal.
HTML5 and CSS3 Fundamentals

Build a website from the ground up by first learning the fundamentals of HTML5 and CSS3, the two popular programming languages used to present content online. HTML deals with fonts, colors, graphics, and hyperlinks, while CSS describes how HTML elements are to be displayed.

floezemAuthor Commented:
By the way i have been learning pascal for under one month, so simple procedures is as far as i can do!
No accusation. I was just the first to mention it (look up almost any topic which would be a first semester assignment and you'll find people looking to have the problem solved for them. It is a big enough problem that it is mentioned in the membership agreement ( - search for "homework"). I apologize if you feel I was "putting you down"; I don't know you so I have no opinion of you, only of your question. I agree with your attitude in your follow-up that learning the material is more important than any particular assignment.

Okay, you need nelp getting started. What do you know in Pascal? I am going to assume you know how to declare an array type. Have you previously declared a two-dimensional array? A two dimensional array is an array of arrays. Let's consider a topological map of a small country (since you're doing homework I am NOT going to provide any code directly related to the assignment; I will help with assignment design (as I did in my first post) and answer any Pascal level questions you ask but any code examples I provide will be "unrelated" to your assignment. I put that in quotes because all code is, at some level, related). We have a grid laid over the country at some resoulution, call it 1km and we want to be able to store height information (in integer meters above sealevel) in an array.

We start by looking at a strip (east-to-west) across the country. There are 100 grid units across the country (and 50 grid units north-to-south). We want an array that can hold one strip so we will declare a new type:

        StripType = ARRAY[1..100] OF INTEGER;

So one StripType is required for each of the 50 strips that make up our map. We need another type, say:

        MapType = ARRAY [1..50] OF StripType;

Does this make sense? Now we need to declare a variable of our new type:

        TopoMap : MapType;

So far, so good, we have somewhere to store the topomap. What next? How about reading and writing the 5000 values in the map (cool thing about multi-dimensional arrays is the compact naming for a large number of variables).

One improvement would be to use constants for the sizes of the arrays: easier to change if we needed to (especially since the sizes will be used in loops later on) Lets do that:
  NumberOfColumns = 100;
  NumberOfStrips  = 50;
  StripType = ARRAY[1..NumberOfColumns] OF INTEGER;      
  MapType  = ARRAY[1..NumberOfStrips] OF StripType;

  TopoMap : MapType;

So how to we read one? The following procedure should do the reading. Note that this code compiles but was not tested with data:

PROCEDURE ReadMap(    FName : STRING; VAR Map : MapType );
  InFile : TEXT;
  Column : INTEGER;
  Strip       : INTEGER;
  Assign(InFile, FName);
  { Where in the map are we reading? }
  Column := 1;
  Strip := 1;
    Read(InFile, Map[Strip][Column]);
    Column := Column + 1;
    IF (Column MOD NumberOfColumns = 1) THEN
      Column := 1;
      Strip := Strip + 1;
END; { ReadMap }

But why not use a couple of for loops? After all, we know the number of rows and columns, right? But if we do it THIS way, it is much easier to modify for your application where the number of columns and the number of rows is NOT known.

One thing to note: Since the number of characters in any given row is independent of any of the other rows, you're going to need to keep track of the number of characters for each row. Sounds like a one-dimensional array to me.

So, there is some sample code that reads information from a text file into a two dimensional array. If I only told you things you already know or if I started past you, please follow-up and ask specific questions so I know what you need to know.

Hope this helps, -bcl
floezemAuthor Commented:
that was quite helpful but actually what i am really stuck on is on how to get the program to search for a specific character string inside a text file. And yes you went abit too far of me because i really couldnt draw parallels between a map and a search. Sorry i just got started on arrays. Also is there a specific way this text file must be set up, for example its structure and so on. That is why i am asking for a sample of source code + the text file itself in workable format so i can analyze how it works. Anyway since you insist on playing the riddle game, i will just continue accepting any hint you can give me. Going to the lab today and asking my buddy tom to help out again as usual. Maybe you wont agree with me but the best way to learn is go through examples of source codes. Thats for me anyway, probably since i am too dumb to understand what the text says, and too dumb to even know what you're saying eh?
I will follow up later this evening. For the moment, can you show me how you can open a text file and read it character by character. Maybe print one character per line or something. I want to know where you are on that. I'll happily work with you on arrays.

floezemAuthor Commented:
well thats where my problem is, i can open a text file but when i ask it to read from the text an error occurs when i compile it. By the way i saved my text as k.dat. And i use eoln when reading from the text. Thaat is why i am wondering how the text file should be set up to get it working, is there a format to do it? I am sure i got my variables all right. I even tried to modifiy the tet file by assigning variables to it which obviouslyh didnt work.
Well, for a first step, try the following:

(1) Open a text file in Notepad or in your Pascal editor. We will use  the name in.txt (I chose .txt because Notepad wants to put that extension on all files it saves so we will use a name that is easy to save).

Type in the following three lines:

1 2 3 4 5
6 7 8 9 10 11 12
13 14

(2) The following Pascal file will read in.txt when it is run IN THE SAME DIRECTORY WHERE in.txt WAS SAVED.
  X       : INTEGER;
  Total       : INTEGER;
  InFile : TEXT;

  Assign(InFile, 'in.txt');

  Total := 0;
    IF (EOLN(InFile)) THEN
      Writeln('Total = ', Total);
      Total := 0;
    Read(InFile, X);

    Total := Total + X;
  Writeln('After loop: Total = ', Total);

It demonstrates how to read integers from a text file and how to handle the end of the line marker. The program sums the integers found on each line of the file (notice that there are a different number of entries on each line).

Hope this helps,

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
floezemAuthor Commented:
well i already know how to read from a file, like i said i am quite stuck with the searching part. Here is what i came out with, which obviously doesnt work...i have a file called 'text' in my dir, and its a file that contains a paragraph of words. just like a paragraph in a story book. Problem is when i type the word i want to search nothing happens. So tell me is my text file set up right? can it be in the format of a story paragraph, and what is wrong with my coding. If you can correct my mistakes that would be great but if you still find that its cheating well, i am sorry for wasting ur time.
Anyway heres the program with the text file, take a look at it if you will.

Program textsearch(input,output,f1);
   blank = ' ';
   line = array [1..72] of char;
   table = array [1..50] of line;
   lenS : integer;
   S : line;
   STR : table;
   f1 : text;
   c,b,x,y,d : integer;
   find_word : boolean;

   lenS := 0;
   writeln('enter the character or word to look up');
   while not eoln(input) do
      lenS := lenS+1;

   while not eof(f1) do
      b := 1;
      while not eoln(f1) do
         read(f1, STR[c] [b]);
         b := b+1;
      c := c+1;

   find_word := true;
   for x := 1 to 50 do
      for y := 1 to 72 do
         d := 1;
      if STR[x] [y] = s[d] then
         for d:= 2 to lenS do
            if ((S[d]<>STR[x] [y+d-1]) and (y+d-1<=72)) then
               find_word := false;
         if (find_word = true) then
            writeln(S,' is in line ',x);

mary had a little lamb, the lamb was black as coal, the lamb was so black that it vanished in the night, and the lamb has three legs, it is very fat and it is not a virgin like mary, it is a very black bad lamb, and it loves to fetch balls, the real ones not the fakes, and it loves to eat chickens.
floezemAuthor Commented:
as far as i know the poing where u press the return key in the text is the eoln mark but i am really not sure now.
Okay, what is the problem? You show a long program but you don't seem to know where the problem is. You didn't tell me WHAT the problem is.

Try printing the contents of your array to the screen or a file to make sure they are what you expect.
Do the same with S
Print out each line number as you begin it
Print out each pair of characters you compare.

This will generate a lot of data; you might want to print it to a file that you can search in Word/Notepad/Whatever. You want to make sure you're using a short file (maybe shorter than the one you have) so you can follow the comparisons by hand.

There are a few problems with subscripts. You don't  use the number of lines (you only want to search lines you read) and you don't know how many characters there are per line. That means you'll be checking memory you have never initialized. Also, in the search loop's if, reverse the conditions. You want Pascal NOT to check STR[Q] where Q > 72 so test then check the element in the array.

The code looks like it is on the right track. You are in for some work to find where the problem is but it looks like a good start. When it works you can move some of the code out to procedures to make the main program easier to follow (especially when you add other search modes).

Hope this helps.

PS - I will rant about this once and then shut up: Your variable names are terrible. Use MEANINGFUL names like lineNumber or columnNumber or FileContents and SearchPattern rather than b, c, STR and S
The readability of your code will go way, way up.
floezemAuthor Commented:
Ok the problem is i didnt get the output i expect on the screen, i still cant get why it didnt process my input correctly so it would get the output at the end. So i am wondering if my file and text file is set out right or not? i amsuppose to search for a particular word in that file but when i type out the word nothing displays. Did you test out my program and implemented my text file that i provided? By the way i will change the variables later, thats the least of my wories, this is just a rough work and i hadnt added comments or anything yet. What is pressing now is solving the problem i have.
floezemAuthor Commented:
sorry but if you refuse to give me a clear answer or hint, and insist on writing riddles, i will have to find help elsewhere, no offense but i think we are both wasting our time. You didnt even make an effort to check my code how are you suppose to help me? I just feel too much fuss have been made over how to solve this program already, i shoulda just wait till tommorow and ask my tutor in the lab. I can get a straight and forward answer that way. Anyway thx for your time. Not gonna post  anything here ever again.
Sorry, I went to bed after my previous response. Not ignore you, just snoring.

I told you to print out what you are reading in BEFORE you make any effort to search for stuff in it. If you are uncomfortable using text files, that would be where I would assume you are having problems. Just reading the arrays and printing them out eliminates a lot of complexity and lets you get that right. It is also something you are going to have to do in any case so it is making real progress  when you do that.

I know you're frustrated but I will give you one suggestion in any case: Start small. Figure out the smallest piece of code you can write and perform a useful test on. Get that part working. Don't type in a whole solution to the problem before you get a piece working. Lets you concentrate on the local changes to find the problem.

Good luck and I hope you find satisfactory help somewhere.

Heh, I guess you can't please them all.
The real sadness is our missing out on linked-list polynomials, original poster's next assignment ( I really like working with students who have trouple with algebra AND Pascal.

Not to orignial poster: You might want to mention to your internet helpers that you're using GNU Pascal; your code is a little bit off to someone using any version of Turbo Pascal (like the free 5.5 compiler). Of course I am sure floezem is off to greener pastures by now.

Of course I have trouple with spelling.

No comment has been added lately, so it's time to clean up this TA.
I will leave a recommendation in the Cleanup topic area that this question is:

recommend - points bcladd

Please leave any comments here within the next seven days.


Pascal Page Editor
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today

From novice to tech pro — start learning today.