PDA

View Full Version : Programming Help (Delphi + simple principles)

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

bob_doe_nz
26-03-2006, 10:09 PM
well I'm just playing around and tried the following for detecting a space in a sentence.

procedure TForm1.Button1Click(Sender: TObject);
var
i,j,k : integer;
temp_string : string;
temp_string2 : string;

begin
j := 0; //initial start position to copy
temp_string := edit1.Text; //copy the text from the editbox to temp

for i := 0 to length(temp_string) do
begin
if temp_string[i] = #32 then //ascii character for a space
begin
k := (i-1); //end position to copy

temp_string2 := copy(temp_string,j,k);
//above line copies text from start position to end position

j := i+1; //sets j to position after space
end;
end;

end; //end procedure

from there you could add the text into an array of some sort and then use a for loop to find the shortest and longest words and put them into labels on your form. You could use a basic if then statement for that

e.g

if length(temp_string2) > length(temp_string3) then
temp_string3 := temp_string2

same could be done for finding the smallest but with < instead.

For finding the number of spaces you could use an integer variable that increments each time it detects a space.
When integer I reaches the end of the string and no more spaces are detected display that too onto a label.

if i = length(temp_string)
AND temp_string[length(temp_string)] <> #32
then
spaces_label.text := space_temp