Advertisement
gguuppyy

...

Nov 18th, 2023 (edited)
72
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Delphi 3.29 KB | Source Code | 0 0
  1. program ShortestWordFinder;
  2.  
  3. uses
  4.   windows, System.SysUtils;
  5.  
  6. function IsSpaceCharacter(ch: Char): Boolean;
  7. begin
  8.   Result := (ch = ' ') or (ch = #9);
  9. end;
  10.  
  11. function ExtractWords(const Sentence: string): TArray<string>;
  12. var
  13.   Words: TArray<string>;
  14.   CurrentWord: string;
  15.   CurrentWordStartIndex: Integer;
  16.   i: Integer;
  17. begin
  18.   SetLength(Words, 0);
  19.   CurrentWord := '';
  20.   CurrentWordStartIndex := 1;
  21.  
  22.   i := 1;
  23.   while i <= Length(Sentence) do
  24.   begin
  25.     while (i <= Length(Sentence)) and IsSpaceCharacter(Sentence[i]) do
  26.       Inc(i);
  27.  
  28.     CurrentWordStartIndex := i;
  29.  
  30.     while (i <= Length(Sentence)) and not IsSpaceCharacter(Sentence[i]) do
  31.       Inc(i);
  32.  
  33.     CurrentWord := Copy(Sentence, CurrentWordStartIndex, i - CurrentWordStartIndex);
  34.  
  35.     if CurrentWord <> '' then
  36.     begin
  37.       SetLength(Words, Length(Words) + 1);
  38.       Words[Length(Words) - 1] := CurrentWord;
  39.     end;
  40.   end;
  41.  
  42.   Result := Words;
  43. end;
  44.  
  45. function FindShortestWord(const Words: TArray<string>): string;
  46. var
  47.   ShortestWord: string;
  48.   i: Integer;
  49. begin
  50.   ShortestWord := '';
  51.  
  52.   for i := 0 to Length(Words) - 1 do
  53.   begin
  54.     if (ShortestWord = '') or (Length(Words[i]) < Length(ShortestWord)) then
  55.       ShortestWord := Words[i];
  56.   end;
  57.  
  58.   Result := ShortestWord;
  59. end;
  60.  
  61. function GetWordStartPosition(const Sentence: string; const Word: string): Integer;
  62. var
  63.   i, j: Integer;
  64.   WordLength: Integer;
  65. begin
  66.   WordLength := Length(Word);
  67.   i := 1;
  68.  
  69.   while i <= Length(Sentence) - WordLength + 1 do
  70.   begin
  71.     j := 1;
  72.     while (j <= WordLength) and (Sentence[i + j - 1] = Word[j]) do
  73.       Inc(j);
  74.  
  75.     if j > WordLength then
  76.     begin
  77.       Result := i;
  78.       Exit;
  79.     end;
  80.  
  81.     Inc(i);
  82.   end;
  83.  
  84.   Result := -1;
  85. end;
  86.  
  87. function NumberToString(Number: Integer): string;
  88. var
  89.   CurrentDigit: Integer;
  90. begin
  91.   Result := '';
  92.   repeat
  93.     CurrentDigit := Number mod 10;
  94.     Number := Number div 10;
  95.     Result := Chr(Ord('0') + CurrentDigit) + Result;
  96.   until Number = 0;
  97. end;
  98.  
  99. procedure ReadSentence(var Sentence: string);
  100. begin
  101.   WriteLn('Введите предложение:');
  102.   ReadLn(Sentence);
  103. end;
  104.  
  105. procedure PrintResult(const Result: string);
  106. begin
  107.   WriteLn(Result);
  108. end;
  109.  
  110. function FormatShortestWordPosition(const ShortestWord: string; ShortestWordStartIndex: Integer): string;
  111. begin
  112.   if ShortestWordStartIndex <> -1 then
  113.     Result := 'Самое короткое слово: ' + ShortestWord +
  114.       ', начинается с позиции: ' + NumberToString(ShortestWordStartIndex)
  115.   else
  116.     Result := 'В предложении нет слов.';
  117. end;
  118.  
  119. procedure PrintShortestWordPosition(const ShortestWord: string; ShortestWordStartIndex: Integer);
  120. var
  121.   ResultMessage: string;
  122. begin
  123.   ResultMessage := FormatShortestWordPosition(ShortestWord, ShortestWordStartIndex);
  124.   PrintResult(ResultMessage);
  125. end;
  126.  
  127. var
  128.   Sentence: string;
  129.   Words: TArray<string>;
  130.   ShortestWord: string;
  131.   ShortestWordStartIndex: Integer;
  132.  
  133. begin
  134.     SetConsoleCP(1251);
  135.     SetConsoleOutputCP(1251);
  136.   ReadSentence(Sentence);
  137.   Words := ExtractWords(Sentence);
  138.   ShortestWord := FindShortestWord(Words);
  139.   ShortestWordStartIndex := GetWordStartPosition(Sentence, ShortestWord);
  140.   PrintShortestWordPosition(ShortestWord, ShortestWordStartIndex);
  141.  
  142.   ReadLn;
  143. end.
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement