DarkBlade4

26-03-2006, 04:44 PM

Hi,

I'm doing delphi at school and we've got a small application to make that finds the longest word in the sentence, shortest word, and number of words. I can't figure out how to find the shortest and longest words, it keeps giving me exceptions after pressing the ok button a few times (look in the code below). Can someone tell me why this happens or find and alternate way to find the longest and shortest words?

-----------------Procedure for longest/shortest word-----------------------

procedure TMainWin.BitBtn1Click(Sender: TObject);

var temp, biggestWord, tempWord : String;

var spacePositions : array of Integer;

var word : array of String;

var numberWords, i, t, x, p, numSpaces, sentenceLength, arrayPosition : Integer;

begin

arrayPosition := 0;

numSpaces := 0;

biggestWord := '';

numberWords := 1;

temp := Trim(sentenceBox.Text);

sentenceBox.Text := temp;

if (length(temp) > 0) then begin

// Find number of words

for i := 0 to length(temp) do begin

if not (temp[i] = ' ') and (temp[i+1] = ' ') then begin

numberWords := numberWords + 1;

end;

end;

//Find number of spaces and length of sentence

for i := 0 to length(temp) do begin

if (temp[i] = ' ') then begin

numSpaces := numSpaces + 1;

end;

end;

SetLength(spacePositions, numSpaces);

SetLength(word, numberWords);

//Find positions of spaces

for i := 0 to length(temp) do begin

if (temp[i] = ' ') then begin

spacePositions[arrayPosition] := i;

arrayPosition := arrayPosition + 1;

end;

end;

biggestWord := '';

for i := 0 to numberWords - 1 do begin

for t := spacePositions[i] to spacePositions[i+1] do begin

word[i] := word[i] + temp[t];

end;

if (length(word[i]) > length(biggestWord)) then begin

biggestWord := word[i];

end;

MainWin.caption := biggestWord;

end;

//Set captions and text

sentenceLength := length(temp) - numSpaces;

numWords.Caption := IntToStr(numberWords);

sentenceLengthBox.caption := IntToStr(sentenceLength);

numSpacesBox.caption := IntToStr(numSpaces);

end;

end;

any help would be appreciated

I'm doing delphi at school and we've got a small application to make that finds the longest word in the sentence, shortest word, and number of words. I can't figure out how to find the shortest and longest words, it keeps giving me exceptions after pressing the ok button a few times (look in the code below). Can someone tell me why this happens or find and alternate way to find the longest and shortest words?

-----------------Procedure for longest/shortest word-----------------------

procedure TMainWin.BitBtn1Click(Sender: TObject);

var temp, biggestWord, tempWord : String;

var spacePositions : array of Integer;

var word : array of String;

var numberWords, i, t, x, p, numSpaces, sentenceLength, arrayPosition : Integer;

begin

arrayPosition := 0;

numSpaces := 0;

biggestWord := '';

numberWords := 1;

temp := Trim(sentenceBox.Text);

sentenceBox.Text := temp;

if (length(temp) > 0) then begin

// Find number of words

for i := 0 to length(temp) do begin

if not (temp[i] = ' ') and (temp[i+1] = ' ') then begin

numberWords := numberWords + 1;

end;

end;

//Find number of spaces and length of sentence

for i := 0 to length(temp) do begin

if (temp[i] = ' ') then begin

numSpaces := numSpaces + 1;

end;

end;

SetLength(spacePositions, numSpaces);

SetLength(word, numberWords);

//Find positions of spaces

for i := 0 to length(temp) do begin

if (temp[i] = ' ') then begin

spacePositions[arrayPosition] := i;

arrayPosition := arrayPosition + 1;

end;

end;

biggestWord := '';

for i := 0 to numberWords - 1 do begin

for t := spacePositions[i] to spacePositions[i+1] do begin

word[i] := word[i] + temp[t];

end;

if (length(word[i]) > length(biggestWord)) then begin

biggestWord := word[i];

end;

MainWin.caption := biggestWord;

end;

//Set captions and text

sentenceLength := length(temp) - numSpaces;

numWords.Caption := IntToStr(numberWords);

sentenceLengthBox.caption := IntToStr(sentenceLength);

numSpacesBox.caption := IntToStr(numSpaces);

end;

end;

any help would be appreciated