实现字符串的库函数

char* _strcpy(char* dest, const char* src)
{
    assert(dest != NULL && src != NULL);
    if (dest == src)
        return dest;
    char* temp = dest;
    while ((*dest++ = *src++) != '\0');
    return temp;
}

char* _strncpy(char* dest, const char* src, unsigned int n)
{
    assert(dest != NULL && src != NULL);
    char* temp = dest;
    while (n-- && *src != '\0')
        *dest++ = *src++;
    *dest = '\0';
    return temp;
}

char* _strcat(char* dest, const char* src)
{
    assert(dest != NULL && src != NULL);
    char* temp = dest;
    while (*dest != '\0')
        dest++;
    while ((*dest++ = *src++) != '\0');
    return temp;
}

char* _strncat(char* dest, const char* src, unsigned int n)
{
    assert((dest != NULL) && (src != NULL));
    char* temp = dest;
    while (*dest != '\0')
        dest++;
    while (n-- && *src != '\0')
        *dest++ = *src++;
    *dest = '\0';
    return temp;
}
    
int _strlen(const char* str)
{
    assert(str);
    int count = 0;
    while (*str++ != '\0')
        count++;
    return count;
}

int _strcmp(const char* s, const char* t)
{
    assert(s != NULL && t != NULL);
    while ((*s != '\0') && (*t != '\0') && (*s == *t))
    {
        s++;
        t++;
    }
    return (*s - *t);
}

char* _strchr(const char* str, int c)
{
    assert(str != NULL);
    while (*str != '\0')
    {
        if (*str == (char)c)
            return (char*)str;
        str++;
    }
    return NULL;
}

char* _strupr(char* str)
{
    assert(str != NULL);
    char* temp = str;
    while (*str != '\0')
    {
        if (*str >= 'a' && *str <= 'z')
            *str -= 0x20;
        str++;
    }
    return temp;
}

char* _strlwr(char* str)
{
    assert(str);
    char* s = str;
    while (*s != '\0')
    {
        if (*s >= 'A' && *s <= 'Z')
            *s += 0x20;
        s++;
    }
    return str;
}

 

strstr

 1 char * __cdecl strstr (const char * str1, const char * str2)
 2 {
 3         char *cp = (char *) str1;
 4         char *s1, *s2;
 5 
 6         if ( !*str2 )
 7             return((char *)str1);
 8 
 9         while (*cp)
10         {
11                 s1 = cp;
12                 s2 = (char *) str2;
13 
14                 while ( *s1 && *s2 && !(*s1-*s2) )
15                         s1++, s2++;
16 
17                 if (!*s2)
18                         return(cp);
19 
20                 cp++;
21         }
22 
23         return(NULL);
24 
25 }

strtok

 1 #include <stdio.h>
 2 
 3 char * ___strtok;
 4 
 5 size_t strspn(const char *s, const char *accept)
 6 {
 7         const char *p;
 8         const char *a;
 9         size_t count = 0;
10         
11         for (p = s; *p != '\0'; ++p)
12         {
13                 for (a = accept; *a != '\0'; ++a) 
14                 {
15                         if (*p == *a)
16                                 break;
17                 }
18                 if (*a == '\0')
19                         return count;
20                 ++count;
21         }
22         
23         return count;
24 }
25 
26 char * strpbrk(const char * cs,const char * ct)
27 {
28         const char *sc1,*sc2;
29         
30         for( sc1 = cs; *sc1 != '\0'; ++sc1) {
31                 for( sc2 = ct; *sc2 != '\0'; ++sc2) {
32                         if (*sc1 == *sc2)
33                                 return (char *) sc1;
34                 }
35         }
36         return NULL;
37 }
38 
39 char * strtok(char * s,const char * ct)
40 {
41         char *sbegin, *send;
42         
43         sbegin  = s ? s : ___strtok;
44         if (!sbegin) {
45                 return NULL;
46         }
47         sbegin += strspn(sbegin,ct);
48         if (*sbegin == '\0') {
49                 ___strtok = NULL;
50                 return NULL;
51         }
52         send = strpbrk( sbegin, ct);
53         if (send && *send != '\0')
54                 *send++ = '\0';
55         ___strtok = send;
56         return sbegin;
57 }
58 
59 int main()
60 {
61         char string[] = "words ,;separated by spaces -- and, punctuation!";
62         const char delimiters[] = " .,;:!-";
63         char *token;
64         
65         token = strtok(string, delimiters);
66         printf("%s\n", token);
67         while(token)
68         {
69                 token = strtok(NULL, delimiters);
70                 if(token)
71                         printf("%s\n", token);
72         }
73         
74         return 0;
75 }

 

    原文作者:算法小白
    原文地址: https://www.cnblogs.com/wangpengjie/archive/2013/04/16/3024996.html
    本文转自网络文章,转载此文章仅为分享知识,如有侵权,请联系博主进行删除。
点赞