Is using strlen() in the loop condition slower than just checking for the null character?

前端 未结 8 758
鱼传尺愫
鱼传尺愫 2020-12-07 00:39

I have read that use of strlen is more expensive than such testing like this:

We have a string x 100 characters long.

I think that<

8条回答
  •  粉色の甜心
    2020-12-07 01:17

    Yes your second may not work 100% percent of the time but it would be slighty quite. This is because when using strlen() you have to call the method each time. A better way would be like so

    int strLength = strlen(x);
    for (int i = 0; i < strLength; i++)
    

    Hope this helps.

提交回复
热议问题