strlen
函数 <cstring>
size_t strlen ( const char * str );
获取字符串长度
返回C字符串str的长度。
C字符串的长度由结束空字符决定:C字符串的长度等于字符串开头和结束空字符之间的字符数
(不包括结束空字符本身)。
不应该将其与保存字符串的数组的大小混淆。例如:
char mystr[100]="test string";
定义了一个长度为100个字符的字符数组,但是用于初始化mystr的C字符串长度只有11个字符。
因此,当sizeof(mystr)计算为100时,strlen(mystr)返回11。
在c++中,char_traits::length实现了相同的行为。
☲ 参数
str
C字符串.
☉ 返回值
字符串的长度.
☣ 示例
/* strlen example */
#include <stdio.h>
#include <string.h>
int main ()
{
char szInput[256];
printf ("Enter a sentence: ");
gets (szInput);
printf ("The sentence entered is %u characters long.\n",(unsigned)strlen(szInput));
return 0;
|
输出:
Enter sentence: just testing
The sentence entered is 12 characters long.
🍄 另请参阅