Jump to content


Check out our Community Blogs

nidal

Member Since 07 Apr 2016
Offline Last Active Apr 09 2016 01:07 PM
-----

Posts I've Made

In Topic: Reading longest word within a string.

07 April 2016 - 09:48 AM

hi its my first post on this site i wish that i am welcome here

this is a definition of a function that return the longest word in a string.

function maxword(ch:string):integer;
var n,nmax:integer;
begin
   n:=0;
   nmax:=0;
  for i:=1 to length(ch) do
  begin

   if (upcase(ch[i]) in ['A'..'Z'])then 
{we check if the char is a letter then increment n}
   n:=n+1
   else if (n>nmax)then 
{in case of the char is not a letter we initilise the counter n to 0 but before we check if is n>nmax}
   begin
   nmax:=n;
   n:=0;
   end
   else
{if is n<nmax and ch[i] not in A..Z then just initilise n to 0} 
   n:=0;

  end;
  {till that we can do maxword:=nmax and the function work fine but in particular case
   when the longest word is the last word or the string don't have a delimiter the execution
   will finish with n = a value and nmax = other value or 0(in case we don't have delimeters at all)
   and the last value of n will not compared to the previous value of n so we add those line
   ex 1
   ch:='hi i like fruits'; maxword will finish with n=6 and nmax = 0;
   
   ex 2 
   ch:='understand';maxword will finish with n=10 and nmax = 0;
}
  if n<nmax then
  maxword:=nmax
  else
  maxword:=n;

end;

{2}


Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download