The following loop is very inefficient:
for (int j=0; j
On each iteration, this loop invokes the function strlen() to evaluate its condition. This is highly inefficient because strlen() is CPU intense. Its execution time depends on the length of s. Is there a way to improve this loop's performance? Certainly. Instead of calling strlen() repeatedly, call it only once and cache the result for further calculations:
const int length = strlen(s); // cache the resultfor (int j=0; j