Darse cuenta de la función de strlen strcpy strstr atoi function
int strlen_(const char *m)
{
//char* 是四个字节 char是一个字节 只要偏移指针判断最后一个以/0结束
int len = 0;
while(*m++ != '\0')
{
len++;
}
cout << "长度:" << len << endl;
return len;
}
char* strcpy_(char* a, const char* b)
{
if (a == NULL || b == NULL)
{
return NULL;
}
char* res = a;
while (*b != '\0')
{
*(a++) = *(b++);
}
*a++ = '\0';
return res;
}
const char* strstr_(const char*a,const char* b)
{
const char* s = a;
if (a == NULL || b == NULL)
{
return NULL;
}
while (*s++!='\0')
{
const char* d = a;
const char* f = b;
while (*d==*f && *d!='\0'&&*f!='\0')
{
d++;
f++;
}
if (*f == '\0')
{
return a;//若f先结束
}
if (*d != '\0' && *f == '\0') {
return NULL;//若d先结束而f还没结束,则返回空
}
}
}
int atoi_(const char* str)
{
if (str == NULL)
{
//非法输入
return -1;
}
int index = 0;//记录数组下标
int num = 0;//存放最终要返回的值
int flag = 1;//设置符号标志位
while (isspace(str[index]))//是空格返回非零值(不一定是1),否则返回0
{
index++;//跳过空格字符,不只有空格,还有\n,\r,\f,\t,\v之类的字符
}
if (str[index] == '-')
{
flag = -1; //除空格外遇到的第一个数字是符号,标记为-1
}
//注意:正负号不算作1
if (str[index] == '-' || str[index] == '+')//如果遇到正负号,说明可以往后继续走
{
index++;
}
while (str[index] >= '0' && str[index] <= '9')//如果遇到的数字说明可以继续往下走
{
num = num * 10 + str[index] - '0';
index++;
}
return flag * num;
}