Advertisement
klasscho

Untitled

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