
15 Dec
2007
15 Dec
'07
7:12 p.m.
Andrew Coppin wrote:
Then Mr C++ looked at it and said "OMG! You don't *never* use strlen() inside a loop!" and the second version was writting:
file[FILE_SIZE] = ' '; n = 0; maxLength = 0; while( n < FILE_SIZE ) { while( file[n++] == ' ' ); wStart = n; while( file[n++] != ' ' ); wLength = n - wStart; if( wLength > maxLength ) { longestWordStart = wStart; maxLength = wLength; } } strncpy( longestString , file + longestWordStart , maxLength );
This version takes 0.005 seconds.
Nice. I especially like the way it'll segfault if there is a blank at the end of the file. Roman