首页 > 解决方案 > 函数 strlen() 的返回值?

问题描述

标签: c++string-length

解决方案


If you have a string with 7 characters, the array indexes of the printable characters go from 0 to 6, and strlen() will return 7. a[7] contains the null terminator character.

So if you start your loop from j = len, the first character it prints is the null character, then it will print the printable characters in the remaining iterations. If you start your loop from len-1, it will print only the printable characters.

The extra spacing you're seeing is that null character. On some operating systems, printing a null character has no visible effect, but on your system it apparently prints a space.


推荐阅读