Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- program Project17;
- {$APPTYPE CONSOLE}
- {$R *.res}
- uses
- System.SysUtils;
- procedure ReadFromFile(var Words: String);
- var
- FileName: String;
- IsCorrect, CorrectName: Boolean;
- InFile: Text;
- begin
- repeat
- Writeln('Enter a file name for data entry in the format Name.txt');
- repeat
- Readln(FileName);
- if FileExists(FileName) then
- CorrectName := True
- else
- begin
- Writeln('The file name was entered incorrectly. Try again. Example: Name.txt');
- CorrectName := False;
- end;
- until CorrectName;
- IsCorrect := True;
- Assign(InFile, FileName);
- Reset(InFile);
- if IOResult <> 0 then
- begin
- Writeln(' File does not exist!');
- IsCorrect := False;
- end
- else
- begin
- Read(InFile, Words);
- if length(Words) = 0 then
- begin
- Writeln('File is empty!');
- IsCorrect := False;
- end;
- end;
- until IsCorrect;
- Close(InFile);
- end;
- function NumberOfLetters(var Words: String) : Integer;
- 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);
- NumberOfLetters := Max;
- end;
- function LongestWord(var Words: String) : String;
- Var
- i, j, k, Max: Integer;
- Line: String;
- begin
- 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);
- LongestWord := Line;
- 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 NewFile: TextFile; LongWord: String; Num: Integer);
- var
- i: Integer;
- begin
- Write(NewFile, LongWord);
- Write(NewFile, ' ');
- Write(NewFile, LongWord);
- CloseFile(NewFile);
- Writeln('Saved to file. ');
- end;
- var
- Number: Integer;
- LongWord, Words, FileName: String;
- MyFile, NewFile: TextFile;
- CorrectFile : Boolean;
- begin
- Writeln('This program finds the longest word in the string.');
- ReadFromFile(Words);
- Writeln('Words from file:');
- Write(Words);
- Writeln('');
- Writeln('The longest word or several words:');
- Number := NumberOfLetters(Words);
- LongWord := LongestWord(Words);
- ReadFileOutputName(MyFile);
- WriteToFile(NewFile,LongWord, Number);
- Readln;
- end.
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement