字串函數匯總

來源:互聯網
上載者:User

字串函數匯總
strlen函數實現strlen是電腦C語言函數,計算字串s的(unsigned int型)長度,不包括'\0'在內。strlen所作的僅僅是一個計數器的工作,它從記憶體的某個位置(可以是字串開頭,中間某個位置,甚至是某個不確定的記憶體地區)開始掃描,直到碰到第一個字串結束符'\0'為止,然後返回計數器值。1.1 數組  123456789int my_strlen(char s[]){    int i = 0;    while (s[i] != '\0')    {        i++;    }    return i;}    1.2 指標1234567891011121314//普通指標做法int my_strlen(const char* str){    assert(str);//斷言         int count=0;         while (*str)    {        count++;        str++;    }    return count;} 

//指標減指標得到兩指標之間元素個數(僅限同一數組兩指標)int my_strlen(const char *str){    assert(str);    const char*p = str;//p為起始位置    while (*str)    {        str++;    }    return str - p;}

 

  
//在C語言庫中(即指標減指標)size_t _cdecl strlen(const char* str){    const char *eos = str;         while (*eos++);         return (eos - str - 1);}

 

 2.  strstr函數實現       strstr() 函數搜尋一個字串在另一個字串中的第一次出現。該函數返回字串的其餘部分(從匹配點)。該函數是二進位安全的。 
char* my_strstr(char* str1, char* str2){    assert(str1);    assert(str2);         char* ptr = str1;         while (*ptr)    {        char *p1 = ptr;        char *p2 = str2;                 while (*p2==*p1)        {            p1++;            p2++;            if (*p2 == '\0')                return ptr;        }        ptr++;    }}

 

 3.  strcpy函數實現       它複製以null為退出字元的儲存區區塊到另一個儲存空間區塊內。由於字串在C語言不是首要的資料型態,而是以實現的方式來替代,在儲存空間內以連續的位元組區塊組成,strcpy 可以有效複製兩個配置在儲存空間以指標回傳的字串(字元指標或是字串指標) 
//原void my_strcpy(char* str1, const char* str2){    assert(str1);//檢測指標有效性    assert(str2);         while (*str2)    {        *str1 = *str2;        str1++;        str2++;    }         *str1 = '\0';}

 

 
//最佳化*1void my_strcpy(char* str1,const char* str2){    assert(str1);    assert(str2);         while (*str1++ = *str2++)//簡化代碼    {        ;    }} 

 

 
//最佳化*2///需要鏈式訪問將void換成char*,函數的傳回值作為另一個函數的參數char* my_strcpy(char* str1, const char* str2){    assert(str1);    assert(str2);         char* ret = str1;         while (*str1++ = *str2++)    {        ;    }         return ret;}

 

 ps:附strncpy函數實現,從str2中複製n個字元到str1中 
void my_strncpy(char* str1, const char* str2, int count){    assert(str1);    assert(str2);         while (count)    {        *str1 = *str2;        count--;    }}

 

 4.  strcmp函數實現     字串比較函數,兩個字串自左向右逐個字元相比(按ASCII值大小相比較),直到出現不同的字元或遇'\0'為止 
int my_strcmp(const char*str1,const char*str2){    assert(str1);    assert(str2);    while (*str1 == *str2)    {        if (*str1 == '\0')        {            return 0;        }        str1++;        str2++;    }    return *str1 - *str2;}

 

 //方法二 
int my_strcmp(const char*str1, const char*str2){    assert(str1);    assert(str2);         while (*str1 == *str2)    {        if (*str1 == '\0')        {            return 0;        }        str1++;        str2++;    }         if (*str1 - *str2 > 0)    {        return 1;    }    else    {        return -1;    }}

 

 5.  strcat函數實現        strcat是連接字串的函數。函數返回指標,兩個參數都是指標,第一個參數所指向的記憶體的地址必須能容納兩個字串串連後的大小。 
char* my_strcat(char* str1,const char* str2){    assert(str1);    assert(str2);    char *ret = str1;         while (*str1)    {        str1++;    }         while (*str1++ = *str2++)    {        ;    }    return ret;}

 

 

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.