Programming C, C++, Java, PHP, Ruby, Turing, VB
Computer Science Canada 
Programming C, C++, Java, PHP, Ruby, Turing, VB  

Username:   Password: 
 RegisterRegister   
 longest word in a sentence
Index -> Programming, Turing -> Turing Help
View previous topic Printable versionDownload TopicSubscribe to this topicPrivate MessagesRefresh page View next topic
Author Message
scorpion1087




PostPosted: 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
Sponsor
Sponsor
Sponsor
sponsor
AsianSensation




PostPosted: Thu Nov 06, 2003 2:54 pm   Post subject: (No 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

Andy




PostPosted: Fri Nov 07, 2003 11:32 pm   Post subject: (No subject)

tsk tsk tsk asian, forgot index so soon? Razz
AsianSensation




PostPosted: Sat Nov 08, 2003 4:33 pm   Post subject: (No 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?
Display posts from previous:   
   Index -> Programming, Turing -> Turing Help
View previous topic Tell A FriendPrintable versionDownload TopicSubscribe to this topicPrivate MessagesRefresh page View next topic

Page 1 of 1  [ 4 Posts ]
Jump to:   


Style:  
Search: