Найти длину самого длинного слова в тексте- — Pascal(Паскаль)

uses crt;
const razd:set of char=[' ','.',',',':',';','!'];
var max:integer;
    st,buf,n_max:string;
begin
 max:=-1;
 clrscr;
 readln(st);
 
 repeat
  if (st[1] in razd) or (length(st)=0) then while st[1] in razd do delete(st,1,1)
  else
  begin
   while (not (st[1] in razd)) and (length(st)>0) do
   begin
    buf:=buf+st[1];
    delete(st,1,1);
   end;
 
   if length(buf)>max then
   begin
    max:=length(buf);
    n_max:=buf;
   end;
  buf:='';
 
 end;
 until (length(st)=0) and (length(buf)=0);
 writeln('Самое длинное слово - ',n_max);
 readln;
end.

Вариант 2

uses crt;
var s:string;
    i,k,n,mx,nmx,kmx:byte;
begin
clrscr;
write('Строка:  ');
readln(s);
s:=s+' ';
mx:=0;nmx:=1;
i:=1;
while i<=length(s)do
if s[i]<>' ' then
 begin
  n:=i;k:=1;
  while s[n+k]<>' 'do
  k:=k+1;
  if k>mx then
    begin
     mx:=k;//длина
     nmx:=n;//начало слова
    end;
  i:=i+k+1;
 end
else i:=i+1;
writeln('Самое длинное слово ',copy(s,nmx,mx));
readln
end.

Leave a Comment

− 4 = 3