The strlen function is expensive. For every call it must loop over every character until a nul terminator is found. Therefore, it is very unwise to call it more often than needed. This is bad code:
Code:
for ( int ix = 0; ix < strlen(a_str); ix++)
{
     a_str[ix] = tolower( (unsigned char) a_str[ix] );
}
Lets consider a string 1000 characters in length. strlen will be called 1001 times and loop over 1000 characters each time. That is over one million wasted iterations. If the tolower call and assignment takes the equivalent of 10 iterations we can calculate that the operation takes a massive 10000% of the time it would take if it was written correctly.

Computers may be much faster than they were ten years ago (although sometimes I doubt that), but a hundred times performance hit is still unacceptable. Do we really want C/C++ code running slower than the VB, PHP, Perl, Python, Javascript, punch card and little old lady with typewriter versions combined?

With the frequent use of strlen and the resulting performance wipe out, one would assume that it is very hard to write code that doesn't use strlen in the loop condition. To the contrary, Dr Watson! Writing a loop condition without strlen is actually quite achievable, even for the most experienced coder.

All that is required is to replace the use of strlen with:
Code:
for ( int ix = 0; a_str[ix] != '\0'; ix++)
{
     a_str[ix] = tolower( (unsigned char) a_str[ix] );
}
or the slightly less efficient:
Code:
int len = strlen(a_str);
for ( int ix = 0; ix < len; ix++)
{
     a_str[ix] = tolower( (unsigned char) a_str[ix] );
}
See, it is possible! If you can't remember, feel free to bookmark this page(typically ctrl+d) and copy(ctrl+c) and paste(ctrl+v).

If peer pressure is pushing you towards excessive use of strlen, JUST SAY NO*.


*strlen may be permitted for medical, artistic, electoral, hunting or driving purposes in some states. Please consult your local strlen provider.