1. 程式人生 > >【C語言】字串函式的實現

【C語言】字串函式的實現

  • 求字串的長度
    strlen
  • 長度不受限制的字串函式
    strcpy
    strcat
    strcmp
  • 長度受限制的字串函式
    strncpy
    strncat
    strncmp
  • 字串查詢
    strchr
    strrchr
    strpbrk
    strstr

  • 高階字串查詢
    strspn
    strcspn

求字串的長度strlen

#include <stdio.h>
#include <assert.h>

int my_strlen(const char *str)  //const在這裡修飾是一大亮點 因為strlen函式只需計算字串的長度,
{                               //無需更改,所以加上const修飾,不能隨意改變
int count = 0; assert(str != NULL); // assert()在這裡是第二亮點,斷言的作用,判斷指標是否為空指標 while(*str != '\0') { count++; str++; } return count; } int main() { char *p = "abcdef"; int ch = my_strlen(p); printf("%d\n",ch); return 0; }

長度不受限制的字串

  • strcpy
#include <stdio.h>
#include <assert.h> #include <windows.h> char * my_strcpy(char * str_des, const char * str_source) { assert(str_des); assert(str_source); char * ret = str_des; while (*ret++ = *str_source++) { ; } return str_des; } int main() { char str1[] = "hello world"
; char str2[20] = { 0 }; //將空間全部初始化為0 char *str3 = my_strcpy(str2, str1); printf("%s\n", str2); printf("%s\n", str3); system("pause"); return 0; }
  • strcat
#include <stdio.h>
#include <assert.h>
#include <windows.h>

char * my_strcat(char * str_source, char * str_des)
{
    assert(str_des);
    assert(str_source);
    char *p = str_source;
    while (*p)   //不能寫成*p++當p開始為0還會++
    {
        p++;
    }
    while (*p++ = *str_des++);
    return str_des;
}

int main()
{
    char str[40] = { 0 };
    my_strcat(str,"hello ");
    printf("%s\n",str);
    my_strcat(str,"world!");
    printf("%s\n", str);
    system("pause");
    return 0;
}
  • strcmp字串比較
#include <stdio.h>
#include <assert.h>
#include <windows.h>

//strcmp字串比較
//兩個字串相等返回0,第一個大於第二個返回正數,反之負數
int my_strcmp(const char* str1, const char * str2)
{
    while (*str1 && *str2)
    {
        if (*str1 == *str2)
        {
            str1++;
            str2++;
        }
        else if (*str1 > *str2)
            return 1;
        else
            return -1;
    }
    if (*str1)
        return 1;
    if (*str2)
        return -1;
    return 0;
}

int main()
{
    char str1[] = "a";
    char str2[] = "b";

    int n = my_strcmp(str1, str2);
    printf("%d\n", n);
    system("pause");
    return 0;
}

長度受限制的字串函式

  • strncpy 拷貝n個字元
#include <stdio.h>
#include <assert.h>
#include <windows.h>

char * my_strncpy(char * str_des, const char * str_source,int size)
{
    assert(str_des);
    assert(str_source);
    char * ret = str_des;    //定義一個指標要拷貝的空間首地址
    while (size--)
    {
        *ret++ = *str_source++;
    }
    return str_des;
}

int main()
{
    char str1[] = "hello world";
    char str2[20] = { 0 };    //將空間全部初始化為0
    char *str3 = my_strncpy(str2, str1,4);

    printf("%s\n", str2);
    printf("%s\n", str3);

    system("pause");
    return 0;
}
  • strncat
    將目標字串的指定長度的字元與原字串連結起來
#include <stdio.h>
#include <assert.h>
#include <windows.h>

char * my_strncat(char * str_source, char * str_des,int size)
{
    assert(str_des);
    assert(str_source);
    char *p = str_source;
    while (*p)   //不能寫成*p++當p開始為0還會++
    {
        p++;
    }
    while (size--)
    {
        *p++ = *str_des++;
    }
    return str_des;
}

int main()
{
    char str[40] = { 0 };
    my_strncat(str, "hello ",3);
    printf("%s\n", str);
    my_strcat(str, "world!",2);
    printf("%s\n", str);
    system("pause");
    return 0;
}
  • strncmp
    字串前n個字元比較
#include <stdio.h>
#include <assert.h>
#include <windows.h>

int my_strncmp(const char* str1, const char * str2,int size)
{
    while (size--)
    {
        if (*str1 == *str2)
        {
            str1++;
            str2++;
        }
        else if (*str1 > *str2)
            return 1;
        else
            return -1;
    }
    return 0;
}

int main()
{
    char str1[] = "abcd";
    char str2[] = "abce";

    int n = my_strncmp(str1, str2,3);
    printf("%d\n", n);
    system("pause");
    return 0;
}

字串查詢

  • strchr
    找一個字元在字串中出現的位置
#include <stdio.h>
#include <assert.h>
#include <windows.h>

//找一個字元在一個字串出現的位置
char * my_strchr(const char *str, char p)
{
    while (*str)
    {
        if (*str == p)
            return str;
        str++;
    }
    return NULL;
}
int main()
{
    char str[] = "abcd";
    char * p = my_strchr(str,'x');
    printf("%s\n", p);
    system("pause");
    return 0;
}
  • strrchr
    找出字串中一個字元最後一次出現的位置
#include <stdio.h>
#include <assert.h>
#include <windows.h>

char * my_strrchr(char * str, char x)
{
    char *p = str + strlen(str) - 1;
    while (p != str-1)
    {
        if (*p == x)
            return p;
        p--;
    }
    return NULL;
}

int main()
{
    char str[] = "This is a sample string ";
    char * ret = my_strrchr(str,'t');
    printf("%s\n", ret);
    system("pause");
    return 0;
}
  • strpbrk
    strpbrk返回一個指標,返回key任何一個字元第一次出現的位置
#include <stdio.h>
#include <assert.h>
#include <windows.h>

char * my_strpbrk(char * str,char*  key)
{
    char *s = str;
    char *k = key;
    while(*s)
    {
        while (*k)
        {
            if (*s == *k)
                return s;
            k++;
        }
        s++;
        k = key;
    }
    return NULL;
}

int main()
{
    char str[] = "This is a sample string ";
    char key[] = "aeiou";
    char *pch = my_strpbrk(str,key);
    while (pch != NULL)
    {
        printf("%c ", *pch);
        pch = my_strpbrk(pch+1, key);
    }
    system("pause");
    return 0;
}
  • strstr

在字串str1中找出str2子串,返回str1中str2的位置

#include <stdio.h>
#include <assert.h>
#include <windows.h>

strstr在字串str1中找出str2子串,返回str1中str2的位置
char* my_strstr(char * str1, char * str2)
{
    char *pa1 = str1;
    char *pb1 = pa1;
    char *pa2 = str2;
    if (str2 == NULL)
        return NULL;
    while (*pa1)
    {
        pb1 = pa1;
        pa2 = str2;
        while (*pb1 && *pa2 && *pb1 == *pa2)
        {
            pb1++;
            pa2++;
        }
        if ('\0' == *pa2)
            return pa1;
        pa1++;
    }
    return NULL;
}


int main()
{
    char str1[] = "123abc889";
    char str2[] = "7";
    char *p = my_strstr(str1, str2);
    printf("%s\n", p);
    system("pause");
    return 0;
}

高階查詢

  • strspn
    返回字串str1中的字元在字串str2中出現的個數
int my_strspn(char * str1, char * str2)
{
    int count = 0;
    char * p2 = str2;
    while (*str1)
    {
        while (*p2)
        {
            if (*str1 == *p2)
            {
                count++;
                break;
            }
            p2++;
        }
        p2 = str2;
        str1++;
    }
    return count;
}


int main()
{
    char str1[] = "23d";
    char str2[] = "1234abcd";
    int n= my_strspn(str1, str2);
    printf("%d\n", n);
    system("pause");
    return 0;
}

  • strcspn
strcspn函式 遍歷str1中的字元,直到遇到str2中的字元停下來,返回str1遇到之前遍歷字元數
可以使用strcspn函式,返回str2中字元在str1中第一次出現的地址,用該地址減去str2的地址就是
遍歷的字元數
int my_strcspn(char * str1, char *str2)
{
    char *p = strpbrk(str1, str2);
    return p - str1;
}

int main()
{
    char str1[] = "123abc";
    char str2[] = "7a";
    int n = my_strcspn(str1, str2);
    printf("%d\n", n);
    system("pause");
    return 0;
}