Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- program Project17;
- {$APPTYPE CONSOLE}
- {$R *.res}
- uses
- System.SysUtils;
- procedure ReadFileName(var MyFile: TextFile);
- var
- FName: string;
- CorrectName: Boolean;
- begin
- Writeln('Enter a file name for data entry in the format Name.txt');
- repeat
- Readln(FName);
- if FileExists(FName) then
- CorrectName := True
- else
- begin
- Writeln('The file name was entered incorrectly. Try again. Example: Name.txt');
- CorrectName := False;
- end;
- until CorrectName;
- AssignFile(MyFile, FName);
- Reset(MyFile);
- end;
- function FileCheck(var Words: String) : Boolean;
- var
- FName: String;
- IsCorrect: Boolean;
- InputFile: TextFile;
- TrueString, IsValidInput: Boolean;
- begin
- Assign(InputFile, FName);
- Reset(InputFile);
- TrueString := True;
- while not EOF (Inputfile) do
- begin
- try
- Read(InPutFile, Words);
- except
- TrueString := False;
- end;
- end;
- if Eof(InputFile) then
- IsValidInput := True
- else
- Writeln('File is empty.');
- Reset(InputFile);
- FileCheck := TrueString;
- end;
- procedure ReadAndOutputLine(var Words: String);
- var
- InputFile:text;
- FName: String;
- begin
- Assign(InputFile,FName);
- Reset(InputFile);
- while not EOF (InputFile) do begin
- Readln(InputFile,Words);
- Write(Words);
- end;
- Reset(InputFile);
- end;
- procedure LongestWord(var Words: String);
- Var
- i, j, k, Max: Integer;
- Line: String;
- begin
- i := 1;
- Max := 0;
- repeat;
- j := 0;
- While (Words[i] <> ' ') and (i <= length(Words)) do
- begin
- Inc(i);
- Inc(j);
- end;
- if j > Max then
- Max := j;
- Inc(i);
- until i > length(Words);
- Writeln(Max);
- i := 1;
- repeat;
- j := 0;
- while (Words[i] <> ' ') and (i <= length(Words)) Do Begin
- Line := Line + Words[i];
- Inc(i);
- end;
- if length(Line) = Max then
- Writeln(Line);
- Line := '';
- Inc(i);
- until i > length(Words);
- end;
- procedure ReadFileOutputName(var MyFile: TextFile);
- var
- NewFName: string;
- CorrectName: Boolean;
- begin
- Writeln('Enter a file name for data entry in the format Name.txt');
- repeat
- Readln(NewFName);
- if Copy(NewFName, length(NewFName) - 3, 4) = '.txt' then
- CorrectName := True
- else
- begin
- Writeln('The file name was entered incorrectly. Try again. Example: Name.txt');
- CorrectName := False;
- end;
- until CorrectName;
- AssignFile(MyFile, NewFName);
- Rewrite(MyFile);
- end;
- procedure WriteToFile();
- var
- LongWord: String;
- FName: String;
- OutFile: Text;
- begin
- Assign(OutFile, FName);
- Rewrite(OutFile);
- Writeln(OutFile, 'The longesr word or words:' + LongWord);
- Close(OutFile);
- end;
- var
- Words: String;
- MyFile: TextFile;
- CorrectFile : Boolean;
- begin
- Writeln('This program finds the longest word in the string.');
- ReadFileName(MyFile);
- CorrectFile := FileCheck(Words);
- ReadAndOutputLine(Words);
- Writeln('Your string: ');
- Writeln(Words);
- Writeln('');
- Writeln('The longest word or several words:');
- LongestWord(Words);
- ReadFileOutputName(MyFile);
- WriteToFile();
- Readln;
- end.
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement