Computer Science Canada

longest word in a sentence

Author:  scorpion1087 [ Thu Nov 06, 2003 1:10 pm ]
Post subject:  longest word in a sentence

i need to create a sentence an find the longest word in that sentence.
if you could give me a hand i would be thankfull.

this is my code so far

var SENT : string
put "enter a sentence"
get SENT : *
put "the total length is", length (SENT)

for i : 1 .. length (SENT)
if SENT (i) = "" then
put SENT (i - 1)
end if
put "the number of spaces is", SENT (i-1)
end for

Author:  AsianSensation [ Thu Nov 06, 2003 2:54 pm ]
Post subject: 

ok, well first you have to break down your sentence into words.

run a for loop to break the sentence into words, and have a counter that keeps track of which one is the highest.

code:
var sentence := "This is a sentence."
var word, highestword := ""
var highestnum := 0

for rep : 1 .. length (sentence)
    /*Count only letters*/
    if (sentence (rep) >= "A" and sentence (rep) <= "Z") or (sentence (rep) >= "a" and sentence (rep) <= "z") then
    /*Add the letters to build a word*/
        word += sentence (rep)
    else
    /*If the thing isn't a letter, then we start to compare*/
        if length (word) > highestnum then
            highestnum := length (word)
            highestword := word
        end if
        /*Resetting the word*/
        word := ""
    end if
end for

put highestword
put highestnum


Author:  Andy [ Fri Nov 07, 2003 11:32 pm ]
Post subject: 

tsk tsk tsk asian, forgot index so soon? Razz

Author:  AsianSensation [ Sat Nov 08, 2003 4:33 pm ]
Post subject: 

index? what is this index you speak of?

seriously though, I didn't even think about using index for this, but how is using index faster?


: