klasscho

Untitled

Feb 18th, 2020
156
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.66 KB | None | 0 0
  1. program Project17;
  2.  
  3. {$APPTYPE CONSOLE}
  4.  
  5. {$R *.res}
  6.  
  7. uses
  8. System.SysUtils;
  9.  
  10.  
  11. procedure ReadFromFile(var Words: String);
  12. var
  13. FileName: String;
  14. IsCorrect: Boolean;
  15. InFile: Text;
  16. begin
  17. repeat
  18. Writeln('Enter the name of the file from which you want to read information: ');
  19. Readln(FileName);
  20. FileName := FileName + '.txt';
  21. IsCorrect := True;
  22. Assign(InFile, FileName);
  23. Reset(InFile);
  24. if IOResult <> 0 then
  25. begin
  26. Writeln(' File does not exist!');
  27. IsCorrect := False;
  28. end
  29. else
  30. begin
  31. Read(InFile, Words);
  32. if length(Words) = 0 then
  33. begin
  34. Writeln('File is empty!');
  35. IsCorrect := False;
  36. end;
  37. end;
  38. until IsCorrect;
  39. Close(InFile);
  40. end;
  41.  
  42. procedure LongestWord(var Words: String);
  43. Var
  44. i, j, k, Max: Integer;
  45. Line: String;
  46. begin
  47. i := 1;
  48. Max := 0;
  49. repeat;
  50. j := 0;
  51. While (Words[i] <> ' ') and (i <= length(Words)) do
  52. begin
  53. Inc(i);
  54. Inc(j);
  55. end;
  56. if j > Max then
  57. Max := j;
  58. Inc(i);
  59. until i > length(Words);
  60. Writeln(Max);
  61. i := 1;
  62. repeat;
  63. j := 0;
  64. while (Words[i] <> ' ') and (i <= length(Words)) Do Begin
  65. Line := Line + Words[i];
  66. Inc(i);
  67. end;
  68. if length(Line) = Max then
  69. Writeln(Line);
  70. Line := '';
  71. Inc(i);
  72. until i > length(Words);
  73. end;
  74.  
  75. procedure ReadFileOutputName(var MyFile: TextFile);
  76. var
  77. NewFName: string;
  78. CorrectName: Boolean;
  79. begin
  80. Writeln('Enter a file name for data entry in the format Name.txt');
  81. repeat
  82. Readln(NewFName);
  83. if Copy(NewFName, length(NewFName) - 3, 4) = '.txt' then
  84. CorrectName := True
  85. else
  86. begin
  87. Writeln('The file name was entered incorrectly. Try again. Example: Name.txt');
  88. CorrectName := False;
  89. end;
  90. until CorrectName;
  91. AssignFile(MyFile, NewFName);
  92. Rewrite(MyFile);
  93. end;
  94.  
  95. procedure WriteToFile();
  96. var
  97. LongWord: String;
  98. FName: String;
  99. OutFile: Text;
  100. begin
  101. Assign(OutFile, FName);
  102. Rewrite(OutFile);
  103. Writeln(OutFile, 'The longesr word or words:' + LongWord);
  104. Close(OutFile);
  105. end;
  106.  
  107.  
  108. var
  109. Words, FileName: String;
  110. MyFile: TextFile;
  111. CorrectFile : Boolean;
  112. begin
  113. Writeln('This program finds the longest word in the string.');
  114. ReadFromFile(Words);
  115. Writeln('Words from file:');
  116. Write(Words);
  117. Writeln('');
  118. Writeln('The longest word or several words:');
  119. LongestWord(Words);
  120. ReadFileOutputName(MyFile);
  121. WriteToFile();
  122. Readln;
  123. end.
Advertisement
Add Comment
Please, Sign In to add comment