简体   繁体   中英

find the longest word in a string without using the split method

My teacher asked us to try and build a function that finds the longest length of a word in a string without using the str.split() method. the function should take a string as a parameter and and output the length of the longest word.

so far I have tried to make a function that puts the string into an array using the .push method then try to break up that array into each individual word and find the length of each word in each sub array and compare then. Because I am not smart or experienced enough to do this a friend of mine told me to try and :

> "just use two “tracking” variables. One to keep track of the current word and one to keep track of the longest word seen so far I will let you think about the initial values of what these should be. Next, it is just a matter of iterating through each character of str (strings are iterable just like arrays) to “build” a word. When you come across a space character or then end of str you know you have a complete word. You compare the length of this word with the current longest word (your other tracking variable) and assign the larger of the two to your longest word tracking variable. At the very end of the iteration your longest word tracking variable will contain the longest word length which you can return.Note: You will need to reset the word variable after you check it's length, so it does not continue “building” through the rest of the iteration." >

I'm not really sure what he means or what to do.

so far I have this :

function findLongestWordLength(str) {
    let words = []

    words.push(str);



    console.log(words)



}

findLongestWordLength("The quick brown fox jumped over the lazy dog");

what should I do ? I have the string pushed to an array but now that my friend has given me a hint I'm not sure what to do.

Loop through each character in the string , adding to the current word. If you get to a space, check if your current word is longer than the previous longest word. If it is, store it. If it isn't, do nothing with it.

Clear out the current word and continue moving through the string.

 function findLongestWordLength(str) { let longestWord = ""; let currentWord = ""; //Move through the string letter-by-letter for (let i = 0; i < str.length; i++) { if (str.charAt(i) === " ") { //If we're at a space character if (currentWord.length > longestWord.length) longestWord = currentWord; //Check if that word was the longest currentWord = ""; //Reset the current word } else { currentWord += str.charAt(i); //Not at a space character, still building the current word } } if (currentWord > longestWord) longestWord = currentWord; //End of string - check current word once more return longestWord; } const longest = findLongestWordLength("The quick brown fox jumped over the lazy dog"); console.log(longest); 

Probably a shorter and quicker way is to use a simple regex like this

 function findLongestWordLength( str ) { var longestWord = ""; str.replace( /\\S+/g, function( match ) { if ( match.length > longestWord.length ) longestWord = match; } ); return longestWord; // .length; } console.log( findLongestWordLength("The quick brown fox jumped over the lazy dog") ); 

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM