本文实例讲述了C字符串操作函数实现方法。分享给大家供大家参考。具体如下:
下面是部分C字符串操作函数的实现,或许在某些笔试时可以用到!
#ifndef NULL#define NULL ((void *)0)#endif/* memcpy的实现代码*/void* memcpy(void *pDst, void *pSrc, int iLen){ char *pTmp = (char *)pDst; char *pTmp2 = (char *)pSrc; if(0 == iLen) return pDst; while(iLen--) *pTmp++ = *pTmp2++; return pDst;}/* memset的实现代码*/void* memset(void *pDst, int iSet, int iLen){ char *pTmp = (char *)pDst; if(0 == iLen) return pDst; while(iLen--) *pTmp++ = (char)iSet; return pDst;}/* strcpy的实现代码*/char *strcpy(char *pDst, char *pSrc){ char *pRst = pDst; do *pDst++ = *pSrc; while(*pSrc++); return pRst;}/* strcat的实现代码*/char *strcat(char *s, char *a){ char *save = s; for(; *s; ++s); while((*s++ = *a++) != 0); return save;}/* strlen的实现代码*/int strlen(char *pStr){ int iLen = 0; while(*pStr++) iLen++; return iLen;}/* strcmp的实现*/int strcmp(char *s, char *t){ for(; *s == *t; s++, t++) { if(('/0' == *s) || ('/0' == *t)) { if(*s == *t) return 0; else break; } } return ((*s > *t) ? 1 : -1);}/* strncmp的实现*/int m_strncmp(char *s, char *t, int n){ if(0 == n) return 0; for (; (--n > 0) && (*s==*t); s++,t++) { if ('/0'==*s) return 0; } if(*s == *t) return 0; return ((*s > *t) ? 1 : -1);}/* strstr的实现*/char* strstr(char *s, char *find){ char c, sc; unsigned int len; if ((c = *find++) != 0) { len = lzs_strlen(find); do { do { if ((sc = *s++) == 0) return (NULL); } while (sc != c); } while (lzs_strncmp(s, find, len) != 0); s--; } return ((char *)s);}
希望本文所述对大家的C语言程序设计有所帮助。
新闻热点
疑难解答
图片精选