Find the Longest Word in a String lesson

Forget about code for a second. If you are going through a sentence on paper one word at a time, how do you keep track of which word is longest?

I count the characters.

And how do you keep track of which is biggest? You don’t just scream 5! 3! 27! 7! 8! into the void while looking for the longest word in a sentence on a piece of paper.

I count the characters and then I compare between them.

How do you compare them? Computers are very stupid. You need to be very specific when talking to them.

1 Like

after the count I go through all the words and see which count is the biggest.

How do you ‘see which is the biggest’? What do you do?

Would your “compare between them” look something like this?

  • Look at “The”
    • it has 3 characters
    • 3 is the longest so far
  • Look at “quick”
    • it has 5 characters
    • 5 is more than 3
    • 5 is the longest so far
  • Look at “brown”
    • it has 5 characters
    • 5 is the same as 5
    • 5 is the longest so far
  • Look at “fox”
    • it has 3 characters
    • 3 is less than 5
    • 5 is the longest so far

Well I count, all chars, then I go through the words and compare.
is word a bigger than word b etc.

COMPARE HOW?!? Like I said, computers are stupid. They need small, simple, precise instructions.

Pretend that you are teaching an alien who has never seen words before. What do you need them to do to find the largest word? Step by tiny step?

There isn’t a command you can punch into the computer for “see what’s bigger”. You need to break down the task.

again sorry not sure what you mean, if I answer to you as a human.
I see words.
I count chars
I get numbers, the largest number wins. thats my word I yell it.

how does the large number wins, I compare it between the smaller numbers.

Ok. I’m clearly not making any sense to you. I’m sorry I couldn’t help.

1 Like

not as precisely but yeah you generalized it.

Right, you need to turn that into code…

The “Look at ‘word’” parts you have already done by making the for loop.

You’re going to need some variable outside the loop to keep track of the length of the longest word. You could initialize it to 0.

Inside the loop, you’re going to need to get the length of the word (as you said earlier, look at its length property). Then, you’ll need to compare that length to the above variable. You learned about comparing numbers here. If the word’s length is greater than the above variable, then you’ll need to overwrite the above variable.

that max variable, how can I make it that it is the max word? let’s say 6 as an example ( ofc i cant give it value of 6)

Have you tried anything? Asking us to write the entire algorithm for you doesn’t really help you learn how to solve problems.

Not yet im thinking about it first.

You are asking us to do all of the thinking for you. That doesn’t help you at all. You need to try things.

maxLength = word[i].length;

I have this for example. but this gives me the length of everything not the biggest for example. so I need to make sure maxlength aims at the maximum word.

That’s part of some useful code. But what if words[i-1] was bigger than words[i]?