日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

【C语言】str类与men库函数的实现(如:strcpy,strcmp,strstr,strcat,memmove,memcpy)

發布時間:2023/11/30 编程问答 33 豆豆
生活随笔 收集整理的這篇文章主要介紹了 【C语言】str类与men库函数的实现(如:strcpy,strcmp,strstr,strcat,memmove,memcpy) 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

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_++;??
  • ????}??
  • ????//即使此時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的優勢所在。我們分情況即可解決。

    代碼實現:

    [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)的全部內容,希望文章能夠幫你解決所遇到的問題。

    如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。