收藏 分销(赏)

常见C字符串处理函数的源代码.doc

上传人:仙人****88 文档编号:9496257 上传时间:2025-03-28 格式:DOC 页数:7 大小:30KB 下载积分:10 金币
下载 相关 举报
常见C字符串处理函数的源代码.doc_第1页
第1页 / 共7页
常见C字符串处理函数的源代码.doc_第2页
第2页 / 共7页


点击查看更多>>
资源描述
常见C字符串处理函数的源代码 以下是一些常见C字符串处理函数的源代码,,学习它一个好处是加深理解C语言的理解,另一个好外是应聘面试中的笔试常常会用到它们, 当然,还有一些没有列出来,或者有更好的实现方法,希望大家一起分享一下share your code! :) //stirng.c=============================================================== 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; } int strlen(const char *str) { assert(str != NULL); int len = 0; while (*str ++ != '\0') ++ len; return len; } char *strdup(const char *strSrc) { assert(strSrc != NULL); int len = 0; while (*strSrc ++ != '\0') ++ len; char *strDes = (char *) malloc (len + 1); while ((*strDes ++ = *strSrc ++) != '\0') NULL; return strDes; } char *strcpy(char *strDes, const char *strSrc) { assert((strDes != NULL) && (strSrc != NULL)); char *address = strDes; while ((*strDes ++ = *strSrc ++) != '\0') NULL; return address; } char *strchr_(char *str, int c) { assert(str != NULL); while ((*str != (char) c) && (*str != '\0')) str ++; if (*str != '\0') return str; return NULL; } char *strchr(const char *str, int c) { assert(str != NULL); for (; *str != (char) c; ++ str) if (*str == '\0') return NULL; return (char *) str; } int strcmp(const char *s, const char *t) { assert(s != NULL && t != NULL); while (*s && *t && *s == *t) { ++ s; ++ t; } return (*s - *t); } char *strstr(const char *strSrc, const char *str) { assert(strSrc != NULL && str != NULL); const char *s = strSrc; const char *t = str; for (; *t != '\0'; ++ strSrc) { for (s = strSrc, t = str; *t != '\0' && *s == *t; ++s, ++t) NULL; if (*t == '\0') return (char *) strSrc; } return NULL; } char *strncpy(char *strDes, const char *strSrc, int count) { assert(strDes != NULL && strSrc != NULL); char *address = strDes; while (count -- && *strSrc != '\0') *strDes ++ = *strSrc ++; return address; } char *strncat(char *strDes, const char *strSrc, int count) { assert((strDes != NULL) && (strSrc != NULL)); char *address = strDes; while (*strDes != '\0') ++ strDes; while (count -- && *strSrc != '\0' ) *strDes ++ = *strSrc ++; *strDes = '\0'; return address; } int strncmp(const char *s, const char *t, int count) { assert((s != NULL) && (t != NULL)); while (*s && *t && *s == *t && count --) { ++ s; ++ t; } return (*s - *t); } char *strpbrk(const char *strSrc, const char *str) { assert((strSrc != NULL) && (str != NULL)); const char *s; while (*strSrc != '\0') { s = str; while (*s != '\0') { if (*strSrc == *s) return (char *) strSrc; ++ s; } ++ strSrc; } return NULL; } int strcspn(const char *strSrc, const char *str) { assert((strSrc != NULL) && (str != NULL)); const char *s; const char *t = strSrc; while (*t != '\0') { s = str; while (*s != '\0') { if (*t == *s) return t - strSrc; ++ s; } ++ t; } return 0; } 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; } int strlen(const char *str) { assert(str != NULL); int len = 0; while (*str ++ != '\0') ++ len; return len; } char *strdup(const char *strSrc) { assert(strSrc != NULL); int len = 0; while (*strSrc ++ != '\0') ++ len; char *strDes = (char *) malloc (len + 1); while ((*strDes ++ = *strSrc ++) != '\0') NULL; return strDes; } int strspn(const char *strSrc, const char *str) { assert((strSrc != NULL) && (str != NULL)); const char *s; const char *t = strSrc; while (*t != '\0') { s = str; while (*s != '\0') { if (*t == *s) break; ++ s; } if (*s == '\0') return t - strSrc; ++ t; } return 0; } char *strrchr(const char *str, int c) {我要论文网 assert(str != NULL); const char *s = str; while (*s != '\0') ++ s; for (-- s; *s != (char) c; -- s) if (s == str) return NULL; return (char *) s; } char* strrev(char *str) { assert(str != NULL); char *s = str, *t = str, c; while (*t != '\0') ++ t; for (-- t; s < t; ++ s, -- t) { c = *s; *s = *t; *t = c; } return str; } char *strnset(char *str, int c, int count) { assert(str != NULL); char *s = str; for (; *s != '\0' && s - str < count; ++ s) *s = (char) c; return str; } void *memmove (void *dest, const void *src, int count) { assert(dest != NULL && src != NULL); void *address = dest; while (count --) {硕士论文 *(char *) dest = *(char *) src; dest = (char *) dest + 1; src = (const char *)src + 1; } return address; } void *memset(void *str, int c, int count) { assert(str != NULL); void *s = str; while (count --) { *(char *) s = (char) c; s = (char *) s + 1; } return str; } 就是这么多了 欢迎补充!
展开阅读全文

开通  VIP会员、SVIP会员  优惠大
下载10份以上建议开通VIP会员
下载20份以上建议开通SVIP会员


开通VIP      成为共赢上传

当前位置:首页 > 教育专区 > 小学其他

移动网页_全站_页脚广告1

关于我们      便捷服务       自信AI       AI导航        抽奖活动

©2010-2025 宁波自信网络信息技术有限公司  版权所有

客服电话:4009-655-100  投诉/维权电话:18658249818

gongan.png浙公网安备33021202000488号   

icp.png浙ICP备2021020529号-1  |  浙B2-20240490  

关注我们 :微信公众号    抖音    微博    LOFTER 

客服