Advertisement
klasscho

Untitled

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