Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- program ShortestWordFinder;
- uses
- windows, System.SysUtils;
- function IsSpaceCharacter(ch: Char): Boolean;
- begin
- Result := (ch = ' ') or (ch = #9);
- end;
- function ExtractWords(const Sentence: string): TArray<string>;
- var
- Words: TArray<string>;
- CurrentWord: string;
- CurrentWordStartIndex: Integer;
- i: Integer;
- begin
- SetLength(Words, 0);
- CurrentWord := '';
- CurrentWordStartIndex := 1;
- i := 1;
- while i <= Length(Sentence) do
- begin
- while (i <= Length(Sentence)) and IsSpaceCharacter(Sentence[i]) do
- Inc(i);
- CurrentWordStartIndex := i;
- while (i <= Length(Sentence)) and not IsSpaceCharacter(Sentence[i]) do
- Inc(i);
- CurrentWord := Copy(Sentence, CurrentWordStartIndex, i - CurrentWordStartIndex);
- if CurrentWord <> '' then
- begin
- SetLength(Words, Length(Words) + 1);
- Words[Length(Words) - 1] := CurrentWord;
- end;
- end;
- Result := Words;
- end;
- function FindShortestWord(const Words: TArray<string>): string;
- var
- ShortestWord: string;
- i: Integer;
- begin
- ShortestWord := '';
- for i := 0 to Length(Words) - 1 do
- begin
- if (ShortestWord = '') or (Length(Words[i]) < Length(ShortestWord)) then
- ShortestWord := Words[i];
- end;
- Result := ShortestWord;
- end;
- function GetWordStartPosition(const Sentence: string; const Word: string): Integer;
- var
- i, j: Integer;
- WordLength: Integer;
- begin
- WordLength := Length(Word);
- i := 1;
- while i <= Length(Sentence) - WordLength + 1 do
- begin
- j := 1;
- while (j <= WordLength) and (Sentence[i + j - 1] = Word[j]) do
- Inc(j);
- if j > WordLength then
- begin
- Result := i;
- Exit;
- end;
- Inc(i);
- end;
- Result := -1;
- end;
- function NumberToString(Number: Integer): string;
- var
- CurrentDigit: Integer;
- begin
- Result := '';
- repeat
- CurrentDigit := Number mod 10;
- Number := Number div 10;
- Result := Chr(Ord('0') + CurrentDigit) + Result;
- until Number = 0;
- end;
- procedure ReadSentence(var Sentence: string);
- begin
- WriteLn('Введите предложение:');
- ReadLn(Sentence);
- end;
- procedure PrintResult(const Result: string);
- begin
- WriteLn(Result);
- end;
- function FormatShortestWordPosition(const ShortestWord: string; ShortestWordStartIndex: Integer): string;
- begin
- if ShortestWordStartIndex <> -1 then
- Result := 'Самое короткое слово: ' + ShortestWord +
- ', начинается с позиции: ' + NumberToString(ShortestWordStartIndex)
- else
- Result := 'В предложении нет слов.';
- end;
- procedure PrintShortestWordPosition(const ShortestWord: string; ShortestWordStartIndex: Integer);
- var
- ResultMessage: string;
- begin
- ResultMessage := FormatShortestWordPosition(ShortestWord, ShortestWordStartIndex);
- PrintResult(ResultMessage);
- end;
- var
- Sentence: string;
- Words: TArray<string>;
- ShortestWord: string;
- ShortestWordStartIndex: Integer;
- begin
- SetConsoleCP(1251);
- SetConsoleOutputCP(1251);
- ReadSentence(Sentence);
- Words := ExtractWords(Sentence);
- ShortestWord := FindShortestWord(Words);
- ShortestWordStartIndex := GetWordStartPosition(Sentence, ShortestWord);
- PrintShortestWordPosition(ShortestWord, ShortestWordStartIndex);
- ReadLn;
- end.
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement