Input: "This is a demo String find the largest word from it" Output: "largest" Input: "Hello guys this is geeksforgeeks where students learn programming" Output: "geeksforgeeks"
To achieve this we use the following approaches:
- Using regex and for..loop
- Using split and sort() method
- Using split and reduce() method
Approach 1: Using regex and for…loop. In this approach, we use regex to split the string into an array of words by using the regex /[a-zA-Z0-9]+/gi and then using for loop iterate the array and search the largest string.
Approach 3 : Using split() and reduce() method. In this approach, we split the string using the String.split() method, and by using the reduce method we search the largest element of the array, which is your largest string.