String processing functions used and summarized by C-database functions, and string functions of c-database functions
1. memcpy (copy memory content)
[Header file] # include <string. h>
[Function prototype] void * memcpy (void * dest, const void * src, size_t len );
[Function description] copy the first len byte of memory content in src to the memory address in dest. Unlike strcpy (), memcpy () completely copies len bytes and does not end with the string Terminator '\ 0.
Return Value: returns the pointer to the dest.
[Note] the memory areas of the src and dest pointers cannot overlap. // Memmove can be used to process overlapping areas.
[Custom implementation]
Void * memcpy (void * dest, const void * src, size_t len) {if (NULL = dest | NULL = src) {return NULL; // (void *) be sure to return a pointer} char * tmpDest = dest; const char * tmpSrc = src; size_t I; for (I = 0; I <len; I ++) {tmpDest [I] = tmpSrc [I];} return dest ;}
2. memmove (copy memory content)
[Header file] # include <string. h>
[Function prototype] void * memmove (void * dest, const void * src, size_t n );
[Function description] memmove () and memcpy () are used to copy the first n Bytes of the memory content specified by src to the address specified by dest. The difference is that when src and dest refer to memory areas overlapping, memmove () can still be correctly processed, but the execution efficiency is slightly slower than memcpy.
Return Value: returns the pointer to the dest.
[Note] the memory areas pointed by the src and dest pointers can overlap.
3. atoi (convert a string to an integer)
[Header file] # include <stdlib. h>
[Function prototype] int atoi (const char * nptr );
[Function description] atoi () scans the nptr parameter string, skips the leading space character, and starts conversion only when a number or positive or negative character is encountered, the conversion ends only when a non-number or string Terminator is encountered, and the result is returned.
Return Value: the number of converted integers.
[Note] the result is the same as that when strtol (nptr, (char **) NULL, 10) is used.
[Custom implementation]
int myAtoi(char *str) { int sign = 1, base = 0, i = 0; while (str[i] == ' ') { i++; } if (str[i] == '-' || str[i] == '+') { sign = 1 - 2 * (str[i++] == '-'); } while (str[i] >= '0' && str[i] <= '9') { if (base > INT_MAX / 10 || (base == INT_MAX / 10 && str[i] - '0' > 7)) { if (sign == 1) return INT_MAX; else return INT_MIN; } base = 10 * base + (str[i++] - '0'); } return base * sign;}
4. strstr (search for the specified string in a string)
[Header file] # include <string. h>
[Function prototype] char * strstr (const char * haystack, const char * needle );
[Function description] strstr () searches for the string needle from the string haystack and returns the address that appears for the first time.
[Return value] returns the address of the first occurrence of the specified string. Otherwise, 0 is returned.
[Custom implementation]
char *strstr(char *haystack, char *needle) { int i, j; for( i=0; i<strlen(haystack); i++ ) for( j=0; j<strlen(needle); j++ ) if ( haystack[i] != needle[j] ) break; if(i>=strlen(haystack)) return NULL; else return &haystack[i];}