int my_atoi(const char* p)
{
assert(p != NULL);
bool b_flag = false;
int n_res = 0;
if(p[0] == '+' || p[0] == '-')
{
b_flag = (*p++ != '+');
}
while(isdigit(*p))
{
n_res = n_res*10 + (*p++ - '0');
}
return b_flag ? -n_res : n_res;
}
char *my_itoa(int val, char *buf, unsigned radix)
{
char *p;
char *firstdig;
char temp;
unsigned digval;
p = buf;
if(val <0)
{
*p++ = '-';
val = (unsigned long)(-(long)val);
}
firstdig = p;
do{
digval = (unsigned)(val % radix);
val /= radix;
if (digval > 9)
*p++ = (char)(digval - 10 + 'a');
else
*p++ = (char)(digval + '0');
}while(val > 0);
*p-- = '\0';
do{
temp = *p;
*p = *firstdig;
*firstdig = temp;
--p;
++firstdig;
}while(firstdig < p);
return buf;
}
char * my_strcpy(char * strDest,const char * strSrc)
{
assert((strDest != NULL) && (strSrc != NULL));
char * strDestCopy=strDest;
while ((*strDest++ = *strSrc++) != '\0');
return strDestCopy;
}
int my_strcmp(const char *str1,const char *str2)
{
while(*str1++ == *str2++)
{
if(*str1 == '\0' && *str2 == '\0')
{
return 0;
}
}
return -1;
}
atoi、itoa、strcpy、strcmp函数的C/C++代码实现
最新推荐文章于 2024-09-03 09:16:55 发布