字符串函数实现
合集下载
相关主题
- 1、下载文档前请自行甄别文档内容的完整性,平台不提供额外的编辑、内容补充、找答案等附加服务。
- 2、"仅部分预览"的文档,不可在线预览部分如存在完整性等问题,可反馈申请退款(可完整预览的文档不适用该条件!)。
- 3、如文档侵犯您的权益,请联系客服反馈,我们会尽快为您处理(人工客服工作时间:9:00-18:30)。
return buffer; } //查找字符串 s 中首次出现字符 c 的位置 char *strchr(char *str, int c) { assert(str != NULL); for (; *str != (char)c; ++ str) if (*str == '\0') return NULL; return str; } //字符串连接 char *strcat(char *strDes, const char *strSrc) { assert((strDes != NULL) && (strSrc != NULL)); char *address = strDes; while (*strDes != '\0') ++ strDes; while ((*strDes ++ = *strSrc ++) != '\0') NULL; return address; } char *strncat(char *strDes, const char *strSrc, unsigned int count) { assert((strDes != NULL) && (strSrc != NULL)); char *address = strDes; while (*strDes != '\0') ++ strDes; while (count -- && *strSrc != '\0' ) *strDes ++ = *strSrc ++; *strDes = '/0'; return address; } //查找字符串第一次出现的位置 char *strstr(const char *strSrc, const char *str) { assert(strSrc != NULL && str != NULL); const char *s = strSrc; const char *t = str; for (; *strSrc != '\0'; ++ strSrc) {
//字符串比较 int strcmp(const char *s, const char *t) { assert(s != NULL && t != NULL); while(*s && *t && *s == *t) { ++ s; ++ t; } return (*s - *t); } //字符串比较(不区分大小写比较,大写字母会被映射为小写字母) int stricmp(const char *dst, const char *src) { assert(s != NULL && t != NULL); int ch1, ch2; while(*dst && *src) { if((ch1 = (int)*dst) >= 'A' && (ch1 <= 'Z')) ch1 += 0x20; if((ch2 = (int)*src) >= 'A' && (ch2 <= 'Z')) ch2 += 0x20; if(ch1 == ch2) { ++ dst; ++ src; } else break; } return(ch1 - ch2); } int strncmp(const char *s, const char *t, unsigned int count) { assert((s != NULL) && (t != NULL)); while (*s && *t && *s == *t && count --) { ++ s; ++ t; } return(*s - *t); }
for (s = strSrc, t = str; *t != '\0' && *s == *t; ++s, ++t) NULL; if (*t == '\0') return (char *) strSrc; } return NULL; } //将字符串拷贝到新的位置 char *strdup_(char *strSrc) { if(strSrc!=NULL) { char *start=strSrc; int len=0; while(*strSrc++!='\0') len++; char *address=(char *)malloc(len+1); assert(address != NULL); while((*address++=*start++)!='\0'); return address-(len+1); } return NULL; }
//memcpy(), 拷贝不重叠的内存块 void* memcpy(void* to, const void* from, size_t count) { assert((to != NULL) && (from != NULL)); void * result = toΒιβλιοθήκη Baidu char * pto = (char *)to; char * pfrom = (char *)from; assert(pto < pfrom || pto > pfrom + count -1); while(count--) { *pto++ = *pfrom++; } return result; } //memmove(), 拷贝重叠或者是不重叠的内存块 void* memmove(void* to, const void* from, size_t count) { assert((to != NULL) && (from != NULL)); void * result = to; char * pto = (char *)to; char * pfrom = (char *)from; //to 与 from 没有重叠 if(pto < pfrom || pto > pfrom + count -1) { while(count--) { *pto++ = *pfrom++; } } //to 与 from 有重叠,从后向前 move else { pto = pto + count -1; pfrom = pfrom + count -1; while(count--) { *pto-- = *pfrom--; } } return result; }
//字符串长度 int strlen(const char *str) { assert(str != NULL); int len = 0; while (*str ++ != '\0') ++ len; return len; } //字符串拷贝 char *strcpy(char *to, const char *from) { assert((to != NULL) && (from != NULL)); char * result = to; while( (*to++ = *from++) != '\0') NULL; return result; } //strncpy(),如果 from 指向的字符个数少于 count,则用'\0'补齐 char *strncpy(char *to, const char *from, size_t count) { assert((to != NULL) && (from != NULL)); char * result = to; while(count--) { if(*from != '\0') { *to++ = *from++; } else { *to++ = '\0'; } } return result; } //memset():把指定内存区域的前 count 个字节设置成字符 c void * memset(void* buffer, int c, size_t count) { assert(buffer != NULL); char * p = (char *)buffer; while(count--) *p++ = (char)c;