https://blog.csdn.net/hanjing_1995/article/details/51539583
strcpy
拷貝源字符串到子字符串,包括‘\0’。
代碼實現:
[cpp]?view plaincopy
char*?strcpy(char*?dst,const?char*?src)??{??????assert(src);??????char*?ret?=?dst;??????while?(*src)??????{??????????*dst?=?*src;??????????src++;??????????dst++;??????}??????*dst?=?'\0';??????return?ret;??}??
2.strncpy:
strncpy與strcpy之間差別在于,strcpy將源字符串全部拷貝到新的字符串中,而strncpy拷貝長度由自己確定。
代碼實現:
[cpp]?view plaincopy
char*?strncpy(char*?dst,?const?char*?src,?int?count)??{??????assert(dst);??????assert(src);??????char*?ret?=?dst;??????while?(count--)??????{??????????*dst?=?*src;??????????dst++;??????????src++;??????}??????*dst?=?'\0';??????return?ret;??}??
3.strcat:
strcat作用是鏈接字符串,即:
str1: hel????str2:lo????則鏈接后為hello。
代碼實現:
[cpp]?view plaincopy
char*?strcat(char*?dst,?char*?src)??{??????assert(dst);??????assert(src);??????char*?ret?=?src;??????while?(*src)??????{??????????src++;??????}??????while?(*dst)??????{??????????*src?=?*dst;??????????dst++;??????????src++;??????}??????*dst?=?'\0';??????return?ret;??}??
4.strcmp:
strcmp用來比較字符串長度。
對兩個字符串自左至右逐個字符相比(按ASCII碼值大小比較),直到出現不同的字符或遇到‘\0’為止。如果全部字符相同,則認為相等;若出現不相同的字符,則以第一個不相同的字符的比較結果為準。
如果兩個字符串都由英文字母組成,則有一個簡單的規律:在英文字典中位置在后面的為“大”,還要特別注意:小寫字母比大寫字母“大”。
返回值:
(1)字符串1=字符串2,返回0
(2)字符串1>字符串2,返回一個正整數
(3)字符串1<字符串2,返回一個負整數。
代碼實現:
[cpp]?view plaincopy
int?strcmp(const?char*?dst,?const?char*?src)??{??????assert(dst);??????assert(src);??????while?(*src&&*dst)??????{??????????if?(*src?==?*dst)??????????{??????????????src++;??????????????dst++;??????????}??????????else??????????{??????????????return?*src?-?*dst?-?'\0';??????????}??????}??????return?*src?-?*dst?-?'\0';??}??
5.strncmp:
與strcmp區別在于:strcmp是針對整個字符串而言,而strncmp針對指定長度。
但是要注意,如果count比兩者字符串長度都短的話,則要跳出循環結束。當長度大于兩者字符串長度時,仍然可以比較出是否相等。
代碼實現:
[cpp]?view plaincopy
int?strncmp(const?char*?dst,?const?char*?src,size_t?count)??{??????assert(dst);??????assert(src);??????while?(count--&&*src&&*dst)??????{??????????if?(*src?==?*dst)??????????{??????????????src++;??????????????dst++;??????????}??????????else??????????{??????????????return?*src?-?*dst?-?'\0';??????????}??????}??????return?*src?-?*dst?-?'\0';??}??
6.strstr:
尋找子字符串,我們在源字符串設置一個指針,用做來當此時確實滿足是子串標志原串的位置,如下面的p。而s1,s2分別用作來遍歷。
代碼實現:
[cpp]?view plaincopy
char*?strstr(const?char*?dst,?const?char*?src)??{??????assert(dst);??????assert(src);??????char*?s1?=?dst;????????char*?p?=?src;??????char*?s2?=?p;????????while?(*s2)??????{??????????s1?=?dst;??????????s2?=?p;??????????while?(*s2?&&?*s1)??????????{??????????????if?(*s2?==?*s1)??????????????{??????????????????s1++;??????????????????s2++;??????????????}??????????????else??????????????{??????????????????p++;???????????????????break;??????????????}??????????}??????????if?(*s1?==?'\0')??????????{??????????????return?p;??????????}??????}??????return?NULL;??}??
7.memcpy:
strcpy完成字符串的拷貝,而對于非字符串類的,卻要用memcpy完成內存拷貝。
代碼實現:
[cpp]?view plaincopy
void*?memcpy(void*?dst,?const?void*?src,?size_t?count)??{??????assert(dst);??????assert(src);??????char*?dst_?=?(char*)dst;??????char*?src_?=?(char*)src;??????while?(count--)??????{??????????*dst_++?=?*src_++;??????}????????????*dst_?=?'\0';??????return?dst;??}??
8.memmove:
memmove在于它可解決內存重疊問題。
如:將1,2,3,4,5,6,7,8中的1,2,3,4移動到3,4,5,6位置。那么則仍然按照memcpy則會,將1移動到3處,2移動到4處,再準備移動3時發現此時的3已經由于被移動到此處的1覆蓋而丟失。4同理。這就是memmove的優勢所在。我們分情況即可解決。
代碼實現:
[cpp]?view plaincopy
void?memmove(void*?dst,?const?void*?src,?size_t?count)??{??????assert(dst);??????assert(src);??????char*?dst_?=?(char*)dst;??????char*?src_?=?(char*)src;??????if?(dst_?>?src_&&dst?<?dst_?+?count)??????{??????????while?(count--)??????????{??????????????*(dst_+count)?=?*(src_+count);??????????????dst_++;??????????????src_++;??????????}??????}??????else??????{??????????while?(count--)??????????{??????????????*dst_?=?*src_;??????????????dst_++;??????????????src_++;??????????}??????}??????*dst_?=?'\0';??????return?dst;??}??
本文出自 “Han Jing's Blog” 博客,請務必保留此出處http://10740184.blog.51cto.com/10730184/1765040
個人分類:?C語言
總結
以上是生活随笔為你收集整理的【C语言】str类与men库函数的实现(如:strcpy,strcmp,strstr,strcat,memmove,memcpy)的全部內容,希望文章能夠幫你解決所遇到的問題。
如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。