Quite a long time ago at the school competition there was a task (a little lower), I could not write it in pascal , but I easily dealt with it in php . But all the same it is interesting how the decision on pascal e would look

Here is the task itself:

A simple declarative sentence is entered (words are separated by a space). Display the word of the greatest length. The program should: a) accept the original sentence from the keyboard b) display the word of the greatest length

Here is my solution on php

$item = $_POST["item"];//введенное предложение $array = explode(" ",$item); $max["length"] = 0; $max["word"] = ""; for($i=0;$i < count($array);$i++) { if(strlen($array[$i]) > $max["length"]) { $max["word"] = $array[$i]; $max["length"] = strlen($array[$i]); } } echo "Первое самое длинное слово: <b>".$max["word"]."[".$max["length"]."]</b>"; } 

    1 answer 1

     program prog1; {$APPTYPE CONSOLE} var i, ws: integer; sentence, wmax: string; begin ReadLn(sentence); sentence := Concat(sentence, ' '); wmax := ''; ws := 1; for i := 1 to Length(sentence) do if (sentence[i] = ' ') then begin if (i - ws > Length(wmax)) then wmax := Copy(sentence, ws, i - ws); ws := i+1; end; WriteLn(wmax); ReadLn; end. 

    Delphi7, if that

    UPD optimized a bit

    UPD2 specifically for the author, a proven option in turbo pascal. We are looking for differences)

     program prog1; var i, ws: integer; sentence, wmax: string; begin ReadLn(sentence); sentence := Concat(sentence, ' '); wmax := ''; ws := 1; for i := 1 to Length(sentence) do if (sentence[i] = ' ') then begin if (i - ws > Length(wmax)) then wmax := Copy(sentence, ws, i - ws); ws := i+1; end; WriteLn(wmax); ReadLn; end. 
    • one
      @oxyage, do not throw karma around, for that there is a button to the left of the answer =) - Sh4dow
    • Thanks for the advice and for the answer :) and yet, I want to see on the pascal the solution - oxyage
    • one
      Hmm, what's the difference?) Well, I'll try to dig out TP, check it out. - Sh4dow
    • Updated the answer. - Sh4dow
    • hmm)) there really are almost no differences, thanks for the help - oxyage