Go Premium for a chance to win a PS4. Enter to Win

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 349
  • Last Modified:

comparing strings from a file

Help need a quick answer to a pascal problem...

procedure CompareFile;
  var
  Counter : Integer;
  ReadFile : TextFile;
  FilStr : FilText;       //array of Char
  FileName : FString;     //array of Char
  FileText : FilByte;     //array of Byte
  CompareStr : StrCom;    //array of Char
  begin
  Counter := 0;
  WriteLn('Please enter name of file you want to compare');
  FileName := CreateName(FileName);        //function using array to imput filename
  WriteLn;
  Write('Enter string you will like to find - ');
  CompareStr := Compare(CompareStr);      //function get input from keyboard  
  AssignFile(ReadFile, FileName);
  Reset(ReadFile);
  While not EOF(ReadFile) do begin
  Counter := Counter + 1;
  Read(ReadFile, FileText[Counter]);
  If FileText[Counter] <> 32 then Read(ReadFile, FilStr[Counter]);
  end;
  If FilStr = CompareStr  then begin
  Write('The word ');
  Write(CompareStr);
  Write('is located in line');
  Write(Counter);
  end;
  end;
 
The above code is a piece of code from a program that takes a textfile and compares a string that the user inputs and compares it  to  the file and when it finds that string it is supposed to print out what line it has occured on and how many times it occurred. My problem is that using only standard pascal, how do I take the users string and compare it to the strings in the file.

I think my problem lies with in my while loop.....
0
delbrad
Asked:
delbrad
  • 8
  • 2
  • 2
1 Solution
 
zirconCommented:
It looks like you're missing the "test" to determine if the string from the file to the string entered by the user.

From what I can tell, this test should be in the following statement:

 If FileText[Counter] <> 32 then Read(ReadFile, FilStr[Counter]);

Does the program run correctly if you replace the 32 in the above statement with the array identifier, CompareStr?  Or am I missing something here?

I'll repost this comment as the answer if it's correct.
0
 
zirconCommented:
Never mind.  Please ignore my previous comment. I think the problem is elsewhere, but I can't quite put my finger on it yet.
0
 
delbradAuthor Commented:
Maybe my code is not right so does anyone out there have any code that allows you to take a string from a file and compare it to what the user word and display how many times that word occurred in the file..

Must be standard pascal please and it must use arrays.
0
Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

 
zirconCommented:
I'll give it a shot, but it may take a little time.
0
 
zirconCommented:
Actually, this looks like a homework problem.  If it is, it is unnethical for me or anyone else to do your homework for you.

I am willing to help you work through the problem, but will not provide complete code.

I have noticed that some of your statements contain incorrect keywords, for instance AssignFile should be Assign.

If your using arrays of characters (strings) to solve this problem, you'll need another loop nested inside of the while loop to do the comparison of the characters of the string read from the file to the string input by the user.
0
 
omsecCommented:
...anyway you could do that much easier directly with Strings.
0
 
omsecCommented:
...anyway you could do that much easier directly with Strings.
0
 
delbradAuthor Commented:
I know this follow on statement will probably get me kick out of expert exchange but it has to be said..

First Zircon this is not a homework assignment, I was just trying to teach myself pascal and second your hollier than thou attitude is not needed I was just looking for a hint on how to do this and not a whole program even though my added comment my have sounded like I was. And finally
maybe you should invest in an delphi  book and you will see that Assignfile and the other keywords are used in object pascal...
0
 
zirconCommented:
1.  No, you won't get kicked out.  You are entitled to your opinion.  You have said nothing so offensive as to get you kicked out.

2.  No reason to get defensive.  Many people do try to cheat on homework here, and many of us try to ensure as little of that happens as possible.

3.  You didn't ask for an answer in object pascal, you asked for an answer in standard pascal.  Assignfile is not used in standard pascal.  If you wanted the answer in Delphi (object Pascal), you should have posted the question in the Delphi area.

4.  I said I was willing to help you work through it.  How is that "Holier than thou..."?

5.  I did develop some code to perform what you asked, but have not had a chance to test it yet.  Two options I see:  I can test it and provide it as an answer (if your still willing to accept help from me, that is), or I can post it now without testing it and follow up with comments if it needs to be debugged or modified.  I would prefer to test it first to save us both some time.  If someone else beats me in posting the answer, so be it.  Sounds like you'd prefer it that way anyway.  Bear in mind, I hold nothing against you for your "attack" on my character.  I'd still like to help you.

Do you have Turbo Pascal, or does your compiler have the functions copy() and length() to copy substrings and determine the length of a string respectively?
0
 
zirconCommented:
First, I do want to apologize to you for assuming this was homework.  In the past I crossed the line too far the other way in giving too much information to someone who was doing homework and subsequently rethought the ethics of the situation (although I didn't give complete code, it was too close to "doing the homework."  It now appears that I've gone to the opposite extreme and have assumed your question was homework when it was not.  I'll learn from this and next time I'll ask the question poster before I make any comments about what is ethical and what is not.  I'm sorry.
0
 
zirconCommented:
The following code was compiled using Turbo Pascal 7.0 on an IBM clone.  It uses strings rather than character arrays, however you can access characters within the string in the same way you would access an element of a character array (using array subscripts on the string identifier).  It is also written as a program rather than a procedure so that I could fully test it.  It runs and does what you asked for.  I am willing to any follow-up questions you may have regarding this code.

I hope this helps and that we can bury our differences and move on.

Hopefully, the indentation will come through when I submit this.

program CompareFile;
  var
    Counter : integer;
    Line: integer;
    i: integer;
    ReadFile: text;
    FilStr: string;
    FileName: string;
    CompareStr: string;
    test: string;
    Found:  boolean;

  begin
    Counter := 0;
    Line := 0;

    Write('Please enter name of file you want to compare - ');
    ReadLn(FileName);
    WriteLn;
    Write('Enter string you will like to find - ');
    ReadLn(CompareStr);

    Assign(ReadFile, FileName);
    Reset(ReadFile);

    Write('The word ', CompareStr, ' is located in line(s) ');

    While not seekeof (ReadFile) do
      begin
        Line := Line + 1;
        Found := false;
        ReadLn(ReadFile, FilStr);

        for i := 1 to length(FilStr) do
          begin
            if CompareStr[1] = FilStr[i] then
              begin
                test := copy(FilStr, i, length(CompareStr));
                if test = CompareStr then
                  begin
                    write (Line, ' ');
                    Counter := Counter +1;
                  end;  {if}
              end;  {if}
          end;  {for}
      end; {while}
    WriteLn;
    Write('The word ', CompareStr, ' appears ', Counter, ' times');
    WriteLn(' in the file ', FileName);
    close(ReadFile);
  end. {main}
0
 
zirconCommented:
Oops.  I've got an extra variable in there that isn't needed or used.  The boolean variable Found is superfluous.
0

Featured Post

[Webinar] Cloud and Mobile-First Strategy

Maybe you’ve fully adopted the cloud since the beginning. Or maybe you started with on-prem resources but are pursuing a “cloud and mobile first” strategy. Getting to that end state has its challenges. Discover how to build out a 100% cloud and mobile IT strategy in this webinar.

  • 8
  • 2
  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now