Solved

comparing strings from a file

Posted on 1998-04-01
12
338 Views
Last Modified: 2010-08-05
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
Comment
Question by:delbrad
  • 8
  • 2
  • 2
12 Comments
 
LVL 1

Expert Comment

by:zircon
ID: 1217429
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
 
LVL 1

Expert Comment

by:zircon
ID: 1217430
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
 

Author Comment

by:delbrad
ID: 1217431
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
 
LVL 1

Expert Comment

by:zircon
ID: 1217432
I'll give it a shot, but it may take a little time.
0
 
LVL 1

Expert Comment

by:zircon
ID: 1217433
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
 
LVL 2

Expert Comment

by:omsec
ID: 1217434
...anyway you could do that much easier directly with Strings.
0
Free Trending Threat Insights Every Day

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

 
LVL 2

Expert Comment

by:omsec
ID: 1217435
...anyway you could do that much easier directly with Strings.
0
 

Author Comment

by:delbrad
ID: 1217436
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
 
LVL 1

Expert Comment

by:zircon
ID: 1217437
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
 
LVL 1

Expert Comment

by:zircon
ID: 1217438
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
 
LVL 1

Accepted Solution

by:
zircon earned 50 total points
ID: 1217439
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
 
LVL 1

Expert Comment

by:zircon
ID: 1217440
Oops.  I've got an extra variable in there that isn't needed or used.  The boolean variable Found is superfluous.
0

Featured Post

Why You Should Analyze Threat Actor TTPs

After years of analyzing threat actor behavior, it’s become clear that at any given time there are specific tactics, techniques, and procedures (TTPs) that are particularly prevalent. By analyzing and understanding these TTPs, you can dramatically enhance your security program.

Join & Write a Comment

Is your Office 365 signature not working the way you want it to? Are signature updates taking up too much of your time? Let's run through the most common problems that an IT administrator can encounter when dealing with Office 365 email signatures.
A Short Story about the Best File Recovery Software – Acronis True Image 2017
Access reports are powerful and flexible. Learn how to create a query and then a grouped report using the wizard. Modify the report design after the wizard is done to make it look better. There will be another video to explain how to put the final p…
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…

747 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

16 Experts available now in Live!

Get 1:1 Help Now