1, Function introduction
This is the third article on rewriting some functions of C language string.c. The following functions have been rewritten in the previous two articles:
strdup String copy strchr Character lookup function (Start from scratch) strrchr Character lookup function(Start from the tail) strcat strncat String splicing function memset Memory initialization function (Can be used to assign initial value) strcmp string comparison strlen Calculate string length strstr String lookup memcmp Memory comparison strcpy String copy memcpy Memory Copy
In this article, we will continue to re create the following functions:
strspn characters finding (Introduce the source code and sample code in detail) strpbrk Character lookup function(Find the first matching character) strtok,strsep String delimitation---Very common in string processing bcopy Memory copy and memcpy() Same function memscan,memchr Find characters in memory area
2, Rewrite function source code
2.1 strspn character lookup
strspn looks up and calculates consecutive characters from the beginning of the parameter s string, which are characters contained in the accept string.
size_t strspn(const char *s, const char *accept) { const char *p; const char *a; size_t count = 0; for (p = s; *p != '\0'; ++p) { for (a = accept; *a != '\0'; ++a) { if (*p == *a) break; } if (*a == '\0') return count; ++count; } return count; } Example: #include <stdio.h> #include <string.h> int main() { int i; char str[] = "123"; char accept[] = "1111123hhhhh123hhhhh123hhhhh123hhhh"; i = strspn(str, accept); printf("Search results: %d\n", i); return 0; }
2.2 strpbrk character lookup function (sequential traversal)
The strpbrk function searches the source string cs for the position that first contains any character in the search string ct and returns it. If it cannot be found, it returns a NULL pointer NULL.
For example, the source string searched is abcd
If the string to be searched is still 78c, the result of the search is C. Because the search string c matches the source string.
char * strpbrk(const char * cs,const char * ct) { const char *sc1,*sc2; for( sc1 = cs; *sc1 != '\0'; ++sc1) { for( sc2 = ct; *sc2 != '\0'; ++sc2) { if (*sc1 == *sc2) return (char *) sc1; } } return NULL; } Example: #include <stdio.h> #include <string.h> int main() { const char str1[] = "1234567890abcdefg"; const char str2[] = "Mbc"; const char *find_str =NULL; find_str = strpbrk(str1, str2); if (find_str) { printf("The first matching character is: %c\n", *find_str); //b } else { printf("No characters found"); } return 0; }
2.3 strtok string separation
strtok function is used to separate strings. Strings can be separated according to specific strings and characters. String processing is commonly used.
The first call to the strtok function replaces all characters in the ct string that appear in s with NULL. Then call strtok(NULL, ct) successively to get each part of the substring. See the following example code for detailed usage.
/** *strtok-Split string into Tags */ char * ___strtok; char * strtok(char * s,const char * ct) { char *sbegin, *send; sbegin = s ? s : ___strtok; if (!sbegin) { return NULL; } sbegin += strspn(sbegin,ct); if (*sbegin == '\0') { ___strtok = NULL; return( NULL ); } send = strpbrk( sbegin, ct); if (send && *send != '\0') *send++ = '\0'; ___strtok = send; return (sbegin); } Sample code: #include <stdio.h> #include <string.h> int main() { char str1[] = "123-456-789-abc"; const char str2[] = "-"; char *find_str =NULL; find_str = strtok(str1, str2); if (find_str) { printf("Delimited string: %s\n", find_str); while (1) { find_str = strtok(NULL, str2); if (find_str) { printf("Delimited string: %s\n", find_str); } else { break; } } } return 0; } /* Delimited string: 123 Delimited string: 456 Delimited string: 789 Delimited string: abc * */
The upgraded version of strtok is the strsep function.
char * strsep(char **s, const char *ct) { char *sbegin = *s, *end; if (sbegin == NULL) return NULL; end = strpbrk(sbegin, ct); if (end) *end++ = '\0'; *s = end; return sbegin; }
2.4 bcopy memory copy function
bcopy has the same function as memcpy, but does not check for NULL endings.
/** *bcopy-Copy one area of memory to another *@src:Copy from where *@dest:Copy to where *@count:The size of the area. bcopy() The same function as memcpy() is used to copy the first n bytes of the memory block It should be noted that bcopy is the same as memcpy(), but the parameters are opposite. */ char * bcopy(const char * src, char * dest, int count) { char *tmp = dest; while (count--) *tmp++ = *src++; return dest; }
2.5 memscan finds characters in memory area
memscan is used to find characters in the memory area. addr is the first memory address to find, c is the character to find, and size is the area range to find
If the search is successful, the address of the character in memory is returned. If it is not found, the first address of the searched memory is returned.
void * memscan(void * addr, int c, size_t size) { unsigned char * p = (unsigned char *) addr; while (size) { if (*p == c) return (void *) p; p++; size--; } return (void *) p; }
memchr function:
Search the character c from the first n bytes of the memory area indicated by s. when the character c is encountered for the first time, stop the search. If successful, return the pointer to the character c; Otherwise, NULL is returned.
void *memchr(const void *s, int c, size_t n) { const unsigned char *p = s; while (n-- != 0) { if ((unsigned char)c == *p++) { return (void *)(p-1); } } return NULL; }