0x00 strlen函数原型
extern unsigned int strlen(char *s);
头文件:string.h
格式:strlen (
字符数组名)
功能:计算给定
字符串的(unsigned int型)长度,不包括'\0'在内
说明:返回s的长度,不包括结束符NULL。
0x01 myStrLen函数
1.定义头文件
#ifndef __STR_FUNC_H__
#define __STR_FUNC_H__
#define ErrMsg(msg){\
do{ printf("%s \n", msg); }while(0);\
}
/******
@@describe: 统计字符串字节数
@@parameter: const char* str 统计字符串
@@return: -1:功能调用失败
*/
int ReacherStrLen(const char* str);
#endif
2.编写函数体
#include "StrFunc.h"
/******
@@describe: 统计字符串字节数
@@parameter: const char* str 统计字符串
@@return: -1:功能调用失败
*/
int ReacherStrLen(const char* str)
{
int str_len = 0;
if (NULL == str)
{
ErrMsg("str is null!");
return -1;
}
while (*str != '\0')
{
++str_len;
++str;
}
return str_len;
}
3.测试函数和实现业务逻辑
int main()
{
int size = ReacherStrLen("reacher");
if (-1 != size)
{
printf("%d \n", size);
}
return 0;
}
0x02 总结编写经验
1.业务接口和业务实现函数分离
2.业务逻辑实现和业务实现函数分离
3.业务实现函数需要进行测试