您好,登錄后才能下訂單哦!
#include <stdio.h>
#include <assert.h>
/*
* 作用:獲取串str的長(zhǎng)度
*/
int strlen(const char *pcStr)
{
assert(pcStr != NULL);
int iLen = 0;
while ('\0' != *pcStr)
{
pcStr++;
iLen++;
}
return iLen;
}
/*
* 前提:strDes指向的內(nèi)存至少能夠容納(strlen(strDes) + strlen(strSrc) + 1) bytes
* 作用:將串strSrc連接到串strDes的末尾,函數(shù)本身不保證安全(使用不當(dāng)會(huì)內(nèi)存溢出)
*/
char *strcat(char *pcStrDest, const char *pcStrSrc)
{
assert((pcStrDest != NULL) && (pcStrSrc != NULL));
char *pcTemp = pcStrDest;
/* pcStrDes指針指向串pcStrDes的末尾 */
while ('\0' != *pcStrDest)
{
pcStrDest++;
}
/* 依次從源串pcStrSrc不斷拷貝字符到目的串pcStrDes中 */
while ('\0' != *pcStrSrc)
{
*pcStrDest = *pcStrSrc;
pcStrSrc++;
pcStrDest++;
}
*pcStrDest = *pcStrSrc;
return pcTemp;
}
/*
* 前提:strDes指向的內(nèi)存至少能夠容納(strlen(strSrc) + 1) bytes
* 作用:將串strSrc拷貝到strDes指向的內(nèi)存空間,函數(shù)本身不保證安全(使用不當(dāng)會(huì)內(nèi)存溢出)
*/
char *strcpy(char *pcStrDest, const char *pcStrSrc)
{
assert((pcStrDest != NULL) && (pcStrSrc != NULL));
char *pcTemp = pcStrDest;
while ('\0' != *pcStrSrc)
{
*pcStrDest = *pcStrSrc;
pcStrSrc++;
pcStrDest++;
}
*pcStrDest = *pcStrSrc;
return pcTemp;
}
/*
* 作用:按字典序比較串s和串t的大小
*/
int strcmp(const char *pcStrA, const char *pcStrB)
{
assert((pcStrA != NULL) && (pcStrB != NULL));
while (('\0' != *pcStrA) && ('\0' != *pcStrB) && (*pcStrA == *pcStrB))
{
pcStrA++;
pcStrB++;
}
return (int)(*pcStrA - *pcStrB);
}
/*
* 前提:strDes指向的內(nèi)存至少能夠容納(strlen(strDes) + count + 1) bytes
* 作用:將串strSrc的前count個(gè)字符連接到串strDes的末尾,函數(shù)本身不保證安全(使用不當(dāng)會(huì)內(nèi)存溢出)
*/
char *strncat(char *pcStrDest, const char *pcStrSrc, int iCount)
{
assert((pcStrDest != NULL) && (pcStrSrc != NULL));
char *pcTemp = pcStrDest;
while ('\0' != *pcStrDest)
{
pcStrDest++;
}
while ((iCount > 0) && ('\0' != *pcStrSrc))
{
*pcStrDest = *pcStrSrc;
pcStrSrc++;
pcStrDest++;
iCount--;
}
*pcStrDest = '\0';
return pcTemp;
}
/*
* 前提:strDes指向的內(nèi)存至少能夠容納(count + 1) bytes
* 作用:將串strSrc的前count個(gè)字符拷貝到strDes指向的內(nèi)存空間,函數(shù)本身不保證安全(使用不當(dāng)會(huì)內(nèi)存溢出)
*/
char *strncpy(char *pcStrDest, const char *pcStrSrc, int iCount)
{
assert((pcStrDest != NULL) && (pcStrSrc != NULL));
char *pcTemp = pcStrDest;
while ((iCount > 0) && ('\0' != pcStrSrc))
{
*pcStrDest = *pcStrSrc;
pcStrSrc++;
pcStrDest++;
iCount--;
}
return pcTemp;
}
/*
* 作用:按字典序比較串s和串t的前count個(gè)字符的大小
*/
int strncmp(const char *pcStrA, const char *pcStrB, int iCount)
{
assert((pcStrA != NULL) && (pcStrB != NULL));
while (('\0' != *pcStrA) && ('\0' != *pcStrB) && (*pcStrA == *pcStrB) &&
(iCount > 0))
{
iCount--;
if (0 == iCount)
{
break;
}
pcStrA++;
pcStrB++;
}
return (int)(*pcStrA - *pcStrB);
}
/*
* 前提:dest指向的內(nèi)存能夠容納count bytes
* 作用:將src所指向內(nèi)存的count bytes拷貝到的dest指向的內(nèi)存
*/
void *memcpy(void *pvDest, const void *pvSrc, int iCount)
{
assert((pvDest != NULL) && (pvSrc != NULL));
void *pvTemp = pvDest;
char *pcDest = (char *)pvDest;
const char *pcSrc = (const char *)pvSrc;
while (iCount > 0)
{
*pcDest = *pcSrc;
pcDest++;
pcSrc++;
iCount--;
}
return pvTemp;
}
/*
* 作用:將str的前count個(gè)字節(jié)置為字符c
*/
void *memset(void *pvSrc, int iCh, int iCount)
{
assert(pvSrc != NULL);
char *pcSrc = (char *)pvSrc;
while (iCount > 0)
{
*pcSrc = (char)iCh;
pcSrc++;
iCount--;
}
return pvSrc;
}
/*
* 作用:將src所指向內(nèi)存的count bytes拷貝到的dest指向的內(nèi)存(解決了memcpy未解決的內(nèi)存重疊問(wèn)題)
*/
void *memmove(void *pvDest, const void *pvSrc, int iCount)
{
assert((pvDest != NULL) && (pvSrc != NULL));
void *pvTemp = pvDest;
char *pcDest = (char *)pvDest;
const char *pcSrc = (const char *)pvSrc;
// 邏輯:
// 1)在src小于dest前提下,然后判斷src+count指向的內(nèi)存是否與dest指向的內(nèi)存重疊
// 2) 如果重疊則從src+count開始復(fù)制,復(fù)制到dest+count,從后往前拷貝count字節(jié)
if ((pvSrc < pvDest) && (pvSrc + iCount > pvDest))
{
pcDest = pcDest + iCount - 1;
pcSrc = pcSrc + iCount - 1;
while (iCount > 0)
{
*pcDest = *pcSrc;
pcDest--;
pcSrc--;
iCount--;
}
}
else
{
while (iCount > 0)
{
*pcDest = *pcSrc;
*pcDest++;
*pcSrc++;
iCount--;
}
}
return pvTemp;
}
int main()
{
int iLen = 0;
int iRet = 0;
char acBuffer[4] = {0};
char *pcTestStr1 = "Today is sunday.";
char acTestStr2[] = "Today";
char acTestStr3[] = "Today is sunday.";
char acTestStr4[sizeof("Today is ") + sizeof("sunday.") - 1] = "Today is ";
iLen = strlen(pcTestStr1);
printf("iLen = %d\n", iLen);
(void)strncpy(acBuffer, pcTestStr1, sizeof(acBuffer) - 1);
printf("acBuffer:%s\n", acBuffer);
iRet = strncmp(pcTestStr1, acTestStr2, strlen(acTestStr2));
printf("strncmp iRet = %d\n", iRet);
iRet = strcmp(pcTestStr1, acTestStr2);
printf("strcmp iRet = %d\n", iRet);
(void)memmove(&acTestStr3[1], &acTestStr3[0], strlen("Today"));
printf("memmove acTestStr3:%s\n", acTestStr3);
printf("before strncat acTestStr4:%s\n", acTestStr4);
(void)strncat(acTestStr4, "sunday.111",
sizeof(acTestStr4) - strlen(acTestStr4) - 1);
printf("after strncat acTestStr4:%s\n", acTestStr4);
return 0;
}
免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如果涉及侵權(quán)請(qǐng)聯(lián)系站長(zhǎng)郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。