•

Check out our Community Blogs

## nidal's Content

There have been 1 items by nidal (Search limited from 19-September 19)

#### See this member's

Sort by                Order

### #682012Reading longest word within a string.

Posted by on 07 April 2016 - 09:48 AM in Pascal and Delphi

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