原型:extern int strlen(char *s);
功能:计算字符串s的(unsigned int型)长度
说明:返回s的长度,不包括结束符NULL。
int strlen(const char *str) { if(!str)return 0; const char *p = str; while(*p++); return p - str - 1; }
int strlenN(const char *str) { unsigned int i = 0; if(!str)return i; for (i = 0; *str != '\0'; i++,*str++); return i; }
原型:extern char *strcat(char *dest,char *src);
功能:把src所指字符串添加到dest结尾处(覆盖dest结尾处的'\0')并添加'\0'。
说明:src和dest所指内存区域不可以重叠且dest必须有足够的空间来容纳src的字符串。
返回指向dest的指针。
char *strcat(char *dest,char *src) { char* cp = dest; while(*cp) cp++; while(*cp++ = *src++); //*cp == '\0'; return(dest); }
int strcmp(const char *str1, const char *str2) { register signed char __res; while(1) { if((__res = *str1 - *str2++) != 0 || !*str1++) break; } if (__res == 0)return __res; __res = __res>0?1:-1; return __res; }
char* strcpy(char* dest,const char* src) { char *tmp = dest; while ((*dest++ = *src++) != '\0');//pde = dest; return tmp; }
char *strncpy(char* dest,const char* src,size_t count) { char *tmp = dest; size_t available = count; while ((*dest++ = *src++) != '\0' && --available > 0); return tmp; }
char *strset(char *s, char c) { char *p = s; while (*s) *s++ = c; return(p); }
void* memcpy(void* pDest, const void* pSrc, unsigned int nCount) { if (pDest == NULL && pSrc == NULL) return pDest; unsigned char* pTmpD = (unsigned char*)pDest + nCount; const unsigned char* pTmpS = (unsigned char*)pSrc + nCount; while (nCount--) { *--pTmpD = *--pTmpS; } return pDest; }
void * memset(void* buffer,char c,size_t count) { char *xs = (char*) buffer; while(count--) *xs++ = c; return buffer; }
void *memchr(const void *buf, char ch, size_t count) { const unsigned char *p = (const unsigned char *) buf; while (count--) { if ((unsigned char)ch == *p++) { return (void *)(p-1); } } return NULL; }
void * memmove(void * dest,const void *src,size_t count) { char *tmp, *s; if (dest <= src /* || (char*)dest >= (char*)src+count */ ) //dest < (char*)src+count 唯一条件 会有带值现象 { tmp = (char *) dest; s = (char *) src; while (count--) *tmp++ = *s++; } //保证dest 所付的值 src不被用到即可 由于反拷贝并 不影响 else // (char*)dest >= (char*)src+count 的现象 也可省略 这个 { //使他在反拷贝中实现 保证 函数的调用速度 tmp = (char *) dest + count; s = (char *) src + count; //保证下标0的位置在首位被用到所以先自减一再赋值 while (count--) *--tmp = *--s; } return dest; }
#pragma pack(push,1) #pragma pack(pop)
#pragma once