程序员的自我修养 8.8分
读书笔记 string.h
超级露

/*CRT字符串相关操作*/

/*这部分功能无须涉及任何与内核交互,是纯粹的用户态的计算*/

char* itoa(int n, char* str, int radix)

{

char digit[] = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ";

char* p = str;

char* head = str;

if (!p || radix < 2 || radix > 36)

return p;

if (radix != 10 && n < 0)

return p;

if (n == 0)

{

*p++ = '0';

*p = 0;

return p;

}

if (radix == 10 && n < 0)

{

*p++ = '-';

n = -n;

}

while (n)

{

*p++ = digit[n % radix];

n /= radix;

}

*p = 0;

for (--p; head<p; ++head,--p)

{

char temp = *head;

*head = *p;

*p = temp;

}

return str;

}

int strcmp(const char* src, const char* dst)

{

int ret = 0;

unsigned char* p1 = (unsigned char*)src;

unsigned char* p2 = (unsigned char*)dst;

while(!(ret = *p1-*p2) && *p2)

++p1, ++p2;

if (ret < 0)

ret = -1;

else if (ret > 0)

ret = 1;

return ret;

}

char *strcpy(char *dest, const char *src)

{

char* ret = dest;

while (*src)

*dest++ = *src++;

*dest = '\0';

return ret;

}

unsigned strlen(const char *str)

{

int cnt = 0;

if (!str)

return 0;

for (; *str!='\0'; ++str)

++cnt;

return cnt;

}

0
《程序员的自我修养》的全部笔记 280篇
豆瓣
我们的精神角落
免费下载 iOS / Android 版客户端