网站超链接,优秀网站推荐,易语言做网站简单教程,广州游戏软件开发公司有哪些字符串是以‘\0’作为结束标志#xff0c;strlen函数的返回值是‘\0’前面的字符串的个数#xff08;不包括‘\0’#xff09;
注意
1#xff0c;参数指向的字符串必须以‘\0’结束
2#xff0c;函数的返回值必须以size_t,是无符号的
使用代码
#includestdio.…字符串是以‘\0’作为结束标志strlen函数的返回值是‘\0’前面的字符串的个数不包括‘\0’
注意
1参数指向的字符串必须以‘\0’结束
2函数的返回值必须以size_t,是无符号的
使用代码
#includestdio.h
#includestring.h
int main()
{char arr[] abcdef;int a strlen(arr);printf(%d, a);return 0;
} 结果运行 strlen模拟实现
方法1
int my_strlen(const char * str)
{int count 0;assert(str);while(*str){count;str;}return count;
}
方法2
int my_strlen(const char * str)
{assert(str);if(*str \0)return 0;elsereturn 1my_strlen(str1);
}
方法3
int my_strlen(char *s)
{assert(str);char *p s;while(*p ! ‘\0’ )p;return p-s;
}整体代码
#includestdio.h
#includeassert.h
int my_strlen(char* arr)
{assert(arr);char* p arr;while (*p! \0)p;return p - arr;
}int main()
{char arr[] abcdef;int a my_strlen(arr);printf(%d, a);return 0;
}