Say I have this array:
$array[] = 'foo'; $array[] = 'apple'; $array[] = '1234567890;
I want to get the length of the longest string in this array. In this case the longest string is 1234567890
and its length is 10.
Is this possible without looping through the array and checking each element?
I wrote this code: char a[100][100] = {"solol","a","1234567","123","1234"}; int max = -1; for(int i=0;i<5;i++) if(max<strlen(a[i])) max=strlen(a[i]); cout<<max; The output it gives is -1. But when I initialize the value of max by 0 instead of 1, the code works fine.
function findLongestWord(str) { var longestWord = str. split(' '). reduce(function(longest, currentWord) { return currentWord. length > longest.
try
$maxlen = max(array_map('strlen', $ary));
Sure:
function getmax($array, $cur, $curmax) { return $cur >= count($array) ? $curmax : getmax($array, $cur + 1, strlen($array[$cur]) > strlen($array[$curmax]) ? $cur : $curmax); } $index_of_longest = getmax($my_array, 0, 0);
No loop there. ;-)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With