1. 程式人生 > >常見字串函式的實現

常見字串函式的實現

strlen(),strcpy()

int my_strlen(const char *str)//字串長度
{
    assert(str != NULL);   
        int len = 0;   
    while (*str ++ != '\0')
    {
        ++ len;
    } 
    return len;   
}

char *my_strcpy(char *di,const char *si)//字串的拷貝
{
    assert((di != NULL) && (si != NULL));
    char * cp = di;
    while
(*si) { *di++ = *si++; } *di= '\0'; return cp; } void main() { char str[100]=""; char *dtr="hello word"; int n=my_strlen(dtr); char *p=my_strcpy(str,dtr); printf("字串長度:%d\n",n); printf("字串拷貝後:%s\n",p); }

strncpy(),strcat(),strncat()

char *my_strncpy
(char *di, const char *si, int n)//字串的長度受限拷貝 { assert((di != NULL) && (si != NULL)); char *ncpy = di; while(n--) { if(*si != '\0') { *di++ = *si++; } else { *di++ = '\0'; } } return ncpy; } char *my_strcat
(char *di, const char *si)//兩個字串的連線 { assert((di != NULL) && (si != NULL)); char *link = di; while (*di != '\0') { ++ di; } while ((*di ++ = *si ++) != '\0') { } return link; } char *my_strncat(char *di, const char *si, int n) // 兩個字串的受限連線 { assert((di != NULL) && (si != NULL)); char *nlink = di; while (*di != '\0') { ++ di; } while (n-- && *si != '\0' ) { *di ++ = *si ++; } *di = '\0'; return nlink; } void main() { char str[100]=""; char ctr[100]="ff"; char ftr[100]="ff"; char *dtr="hello word"; printf("字串長度受限拷貝後:%s\n",my_strncpy(str,dtr,5)); printf("字串連線後:%s\n",my_strcat(ctr,dtr)); printf("字串受限連線後:%s\n",my_strncat(ftr,dtr,7)); }

strchr(),strrchr(),strnchr()

char *my_strchr( char *di, char c)// 查詢字串中首次出現字元c的位置,找到返回指標,找不到返回NULL 
{   
    assert(di != NULL);   
    for (; *di !=c; ++ di)
    {  
        if (*di == '\0') 
        {
            return NULL;
        }   
    }
    return di;   
}
char *my_strrchr( char *si, char c)//查詢字串中最後一次次出現字元c的位置,找到返回指標,找不到返回NULL 
{
    assert(si != NULL);
    char *p = NULL;
    while(*si != '\0')
    {
        if(*si == (char)c)
        {
            p = (char *)si;
        }
        si++;
    }

    return p;
}
char *my_strnchr( const char *di,char c,int n)//字串查詢給定字元,第 n 次出現的位置
{
    assert(di != NULL); 
    int count=0;
    for (; *di !='\0'; ++ di)
    {  
        if (*di==c)
        {
            count+=1;
        }
        if(count==n)
        {
        return (char *)di;
        }
    }
    return NULL;

}
void main()
{
    char *str="hello a waord";
    char *ctr="hello a waard";
    printf("前查詢字元後:%s\n",my_strchr(str,'a'));
    printf("後查詢字元後:%s\n",my_strrchr(str,'a'));
    printf("查詢第n次字元出現後:%s\n",my_strnchr(ctr,'a',3));
}

strstr()

char *my_strstr(const char *di ,const char *si)//查詢字串(子串)   
{   
    assert(di != NULL && si != NULL);   
    const char *s = di;   
    const char *t = si;   
    for (; *di != '\0'; ++ di)   
    {   
        for (s = di, t = si; *t != '\0' && *s == *t; ++s, ++t)
        {   
            NULL;
        }   
        if (*t == '\0')
        {   
            return (char *) di;
        }   
    }   
    return NULL;
} 
void main()
{
    char *str="hello word";
    char *ctr="word";
    printf("%s\n",my_strstr(str,ctr));
}

strcmp(),strncmp()

int my_strcmp(const char *di, const char *si) //字串比較,返回負,零,正數  
{   
    assert(di != NULL && si != NULL);   
    while(*di && *si && *di == *si)   
    {   
        ++ di;   
        ++ si;   
    }
    return (*di - *si);   
}
int my_strncmp(const char *di, const char *si, int n)//受限字串的比較  
{   
    assert((di != NULL) && (si != NULL));   
    while (*di && *si && *di == *si && n --)   
    {   
        ++ di;   
        ++ si;   
    }   
    return(*di - *si);   
}
void main()
{
    char *str="hellobword";
    char *ctr="helloaword";
    printf("%d\n",my_strcmp(str,ctr));
    printf("%d\n",my_strncmp(str,ctr,4));
}

count_chars()

int count_chars(char const *di,char const *si)//在第一個引數中進行查詢, 並返回匹配第二個引數所包含的字元的數量 
{  
    assert((di != NULL) && (si != NULL));
    int count = 0;  
    for (int i = 0;i < strlen(di);i++ )  
    {  
        for(int j = 0;j < strlen(si);j++)  
        {  
            if( di[i] == si[j])  
            {  
                count++;  
            }  
        }  
    }  
    return count;  
} 
void main()
{
    char *str="abcdefg";
    char *ctr="acdfyuilo";
    printf("相同字元數:%d\n",count_chars(str,ctr));
}