zhupolongjoe Posted February 22, 2009 Share Posted February 22, 2009 I know you say I should ask on a matlab community, but I've tried and you guys are way more useful. I need a code that takes a line, which contains some words in it, all words are separated from each other with a space, but there is no other punctuation. The function should give the length of the words...i.e.: lengths('apple grape me') should give 5 5 2 Here is what I have tried, but it does not work...it returns "0" function legnthwords=lengths(line) spaces=findstr(' ', line); index=-1; while index~=spaces; index==index+1 end end Link to comment https://forums.phpfreaks.com/topic/146408-one-more-matlab-problem/ Share on other sites More sharing options...
Mark Baker Posted February 22, 2009 Share Posted February 22, 2009 As before, PHP function to perform this task: function lengthWords($line) { $words = explode(' ',$line); $wordLengths = array(); foreach($words as $word) { $wordLengths = strlen($word); } return $wordLengths; } print_r(lengthWords('apple grape me')); Link to comment https://forums.phpfreaks.com/topic/146408-one-more-matlab-problem/#findComment-768704 Share on other sites More sharing options...
genericnumber1 Posted February 23, 2009 Share Posted February 23, 2009 function [ lengths ] = lengthwords( line ) count = 0; lengths = []; line = [line, ' '] for i = 1 : length(line) if line(i) ~= ' ' count = count + 1 else lengths(end+1) = count count = 0; end end end I hope this isn't homework Link to comment https://forums.phpfreaks.com/topic/146408-one-more-matlab-problem/#findComment-768860 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.