https://blog.csdn.net/hanjing_1995/article/details/51539583
strcpy
拷貝源字符串到子字符串,包括‘\0’。
代碼實現:
- 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拷貝長度由自己確定。
代碼實現:
- 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。
代碼實現:
- 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,返回一個負整數。
代碼實現:
- 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比兩者字符串長度都短的話,則要跳出循環結束。當長度大于兩者字符串長度時,仍然可以比較出是否相等。
代碼實現:
- 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分別用作來遍歷。
代碼實現:
- 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完成內存拷貝。
代碼實現:
- 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_++;??
- ????}??
- ????//即使此時count不為0,但是當我們將原數拷貝到新的數據結束,那也要結束程序。??
- ????*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的優勢所在。我們分情況即可解決。
代碼實現:
- 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