字元函式和字串函式的模擬實現

2021-09-01 06:32:27 字數 4853 閱讀 2747

strlen(算字串的長度)

size_t strlen

(const

char

*str)

;

//1.模擬實現strlen

#include

#include

intmy_strlen

(const

char

*p)return count;

}int

main()

strcpy(字串拷貝)

char

*strcpy

(char

*destination,

const

char

*source)

//strcpy的模擬實現

#include

#include

char

*my_strcap

(char

* dest,

const

char

* src)

intmain()

;char arr2=

"hello bit"

;my_strcap

(arr1, arr2)

;printf

("%s\n"

, arr1)

;return0;

}

strcat(字串追加)

char

*strcat

(char

*destination,

const

char

*source)

#include

#include

char

*my_strcat

(char

*dest,

const

char

*src)

while

(*dest++

=*src++

)return ret;

}int

main()

strcmp(字串比較)比較的是ascll 值

#include

#include

intmy_strcmp

(const

char

*str1,

const

char

*str2)

str1++

; str2++;}

return

*str1 -

*str2;

}int

main()

printf

("%d\n"

, ret)

;return0;

}

長度受限的字元函式:strnc,strncat,strncmp

strncpy

char

*strncpy

(char

*destination,

const

char

*source,size_t num)

;

#include

#include

char

*my_strncap

(char

*dest,

const

char

*src,size_t num)

return ret;

}int

main()

strncat

char

*strncat

(char

*destination,

const

char

*source,size_t num)

;

strncat的模擬實現

#include

#include

#include

char

*my_strncat

(char

* dest,

const

char

* src,size_t num)

while

(num--

)*dest =

'\0'

;return ret;

}int

main()

strncmp

int

strncmp

(const

char

*str1,

const

char

*str2,size_t num)

strncmp的模擬實現

#include

#include

intmy_strncmp

(const

char

* str1,

const

char

*str2,size_t num)if(

*str1 >

*str2)

return1;

else

return-1

;}intmain()

strstr(查詢字串)

char

*strstr

(const

char

*str1,

const

char

*str2)

;

strstr的模擬實現

#include

#include

char

*my_strstr

(const

char

*str1,

const

char

*str2)if(

*s2 ==

'\0'

)return cp;

cp++;}

return

null;}

intmain()

strchr(在字串中找字元)

char

*strchr

(const

char

*str,

char c)

strchr的模擬實現

#include

#include

char

*my_strchr

(const

char

*str,

char c)if(

(char

)c ==

*str)

return

(char

*)str;

return

null;}

intmain()

memcpy(記憶體拷貝)

void

*memcpy

(void

*dest,

const

void

*src,size_t num)

;

#include

#include

void

*my_memcpy

(void

* dest,

const

void

*src,

int count)

return ret;

}int

main()

;int arr2[20]

=;my_memcpy

(arr2, arr1,

sizeof

(arr1));

return0;

}

memmove

void

*memcpy

(void

*dest,

const

void

*src,size_t num)

;

#include

#include

void

*my_memmove

(void

*dest,

const

void

*src,

int count)

}else

}return ret;

}int

main()

;int arr2[20]

=;my_memmove

(arr2, arr1,

sizeof

(arr1));

return0;

}

還有一些字元函式:strtok,strerror,memcmp.

字元轉換函式:tolower(轉小寫),toupper(轉大寫)。

字元函式和字串函式及模擬實現

函式介紹 求字串長度 strlen 長度不受限制的字串函式 strcpy strcat strcmp 長度受限制的字串函式介紹 strncpy strncat strncmp 字串查詢 strstr strtok 錯誤資訊報告 strerror 字元操作 記憶體操作函式 memcpy memmove...

模擬實現字串庫函式

1.strcat 1 函式功能 實現兩個字串的連線 2 思想 首先遍歷目標字串,找到 0 的位址,然後將資源字串通過指標一次一次的拼接在目標字串後面,直到指標走到資源字串的 0 3 char mystrcat char strdestination,const char strsource whil...

字串函式的模擬實現(2)

include includevoid my memcpy void dest,const void src,size t count return ret int main 功能 由src所指記憶體區域複製count個位元組到dest所指記憶體區域。include include includev...