klasscho

Untitled

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