C字符串操作函數(shù)實(shí)現(xiàn)方法小結(jié)
本文實(shí)例講述了C字符串操作函數(shù)實(shí)現(xiàn)方法。分享給大家供大家參考。具體如下:
下面是部分C字符串操作函數(shù)的實(shí)現(xiàn),或許在某些筆試時(shí)可以用到!
#ifndef NULL
#define NULL ((void *)0)
#endif
/*
memcpy的實(shí)現(xiàn)代碼
*/
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的實(shí)現(xiàn)代碼
*/
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的實(shí)現(xiàn)代碼
*/
char *strcpy(char *pDst, char *pSrc)
{
char *pRst = pDst;
do
*pDst++ = *pSrc;
while(*pSrc++);
return pRst;
}
/*
strcat的實(shí)現(xiàn)代碼
*/
char *strcat(char *s, char *a)
{
char *save = s;
for(; *s; ++s);
while((*s++ = *a++) != 0);
return save;
}
/*
strlen的實(shí)現(xiàn)代碼
*/
int strlen(char *pStr)
{
int iLen = 0;
while(*pStr++)
iLen++;
return iLen;
}
/*
strcmp的實(shí)現(xiàn)
*/
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的實(shí)現(xiàn)
*/
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的實(shí)現(xiàn)
*/
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語言程序設(shè)計(jì)有所幫助。
相關(guān)文章
C++多態(tài)特性之派生與虛函數(shù)與模板詳細(xì)介紹
這篇文章主要介紹了C++多態(tài)的特性派生與虛函數(shù)與模板,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)吧2022-09-09
Linux網(wǎng)絡(luò)編程之基于UDP實(shí)現(xiàn)可靠的文件傳輸示例
這篇文章主要介紹了Linux網(wǎng)絡(luò)編程之基于UDP實(shí)現(xiàn)可靠的文件傳輸示例,是很實(shí)用的技巧,需要的朋友可以參考下2014-08-08
C++實(shí)現(xiàn)LeetCode(106.由中序和后序遍歷建立二叉樹)
這篇文章主要介紹了C++實(shí)現(xiàn)LeetCode(106.由中序和后序遍歷建立二叉樹),本篇文章通過簡要的案例,講解了該項(xiàng)技術(shù)的了解與使用,以下就是詳細(xì)內(nèi)容,需要的朋友可以參考下2021-07-07
C++實(shí)現(xiàn)LeetCode(90.子集合之二)
這篇文章主要介紹了C++實(shí)現(xiàn)LeetCode(90.子集合之二),本篇文章通過簡要的案例,講解了該項(xiàng)技術(shù)的了解與使用,以下就是詳細(xì)內(nèi)容,需要的朋友可以參考下2021-07-07
QT Creator+OpenCV實(shí)現(xiàn)圖像灰度化的示例代碼
這篇文章主要為大家詳細(xì)介紹了QT如何利用Creator和OpenCV實(shí)現(xiàn)圖像灰度化效果,文中的示例代碼講解詳細(xì),感興趣的小伙伴可以嘗試一下2022-12-12

