There are many functions in the C language library, among which the string function is a commonly used one, which needs us to master.
catalogue
1. strlen (find string length)
1.1 strlen Simulation Implementation
2.2 simulation implementation strcpy
3. strcat (additional characters)
3.2 simulation implementation of strcat
4. strcmp (compare the contents of two strings)
4.2 simulation implementation of strcmp
5. strstr (returns the pointer of the first occurrence of str1 at str2)
5.1 simulation implementation str
1. strlen (find string length)
This function is to find the length of a string.
Note that it does not count as' \ 0 ', that is, take' \ 0 'as the end flag to calculate the length of the previous string, but it cannot have' \ 0 '
Let's take an interesting example:
int main() { if (strlen("abc") - strlen("abcdef")) printf("hehe"); else printf("haha"); return 0; }
At first glance, the above code is 3 - 6 = - 3 and should output haha
However, the strlen function returns an unsigned integer, so - 3 is regarded as an unsigned integer and should be a number greater than 0.
1.1 strlen Simulation Implementation
So how should this function be implemented if it is written by itself?
Simulation implementation needs to consider all cases:
therefore const Prevent changing strings and assert It is necessary to prevent null pointers from being passed in.
size_t my_strlen(const char* a) { assert(a); int count = 0; while (*a) { count++; a++; } return count; }
2. strcpy (copy string)
This function is used to assign the content of one string to another string. If the destination string has content, it will be directly overwritten.
Note: strcpy copies strings from arr2 to arr1
The copied string must have '\ 0', because this is the end flag
The target space should also be large enough and variable (variable: write with an array rather than a stuck pointer)
int main() { char arr1[] = "xxxxxxxxxxxxxxx"; //char *p = "xxx";// Immutable and small space char arr2[] = "abcd\0efg"; strcpy(arr1, arr2); printf("%s", arr1); return 0; }
2.1 strncpy function
Is this function very similar? Because strcpy directly copies a string completely,
So is there a function that can copy only part of it? This function is used for this purpose.
It takes num characters from the source string and puts them into the destination string.
int main() { char a[] = "abcde"; char b[] = "abc"; strncpy(a, b, 6); printf("%s", a); return 0; }
If the source string is not enough, it will be automatically supplemented with 0
2.2 simulation implementation strcpy
char* my_strcpy(char* a1, const char* a2)//The original pointer is modified without const { assert(a1 && a2); char* ret = a1; while (*a1++ = *a2++) { ; } return ret; }
Let's explain this writing (* a1++ = *a2++ ):
First assign a2 to a1, and then + + respectively. When a2 is 0, the expression value is 0, and then exit the loop.
Because the value is placed in the address, after the address of a1 is saved to ret, the last address returned to ret is the value of a1
3. strcat (additional characters)
Pass in two strings and append the latter string to the previous string.
The source string must have '\ 0' as the end flag, and the target string is large enough and variable. (define the target array size in advance)
If you need to append to yourself, use this function.
3.1 strncat function
This is to append num characters from the source string to the target string, as shown in the following figure:
3.2 simulation implementation of strcat
char* my_strcat(char* a, const char* b) { assert(a && b); char* p = a; //Find a position with string '\ 0' first while (*a) { a++; } while (*a++ = *b++) { ; } return p; }
4. strcmp (compare the contents of two strings)
Pass in two strings and compare the two strings.
STR1 > STR2 returns a positive number; = returns 0; < returns a negative number
So how is it compared?
Remember to compare the different character size, which has nothing to do with the length.
As shown in the figure, the character sizes of 'e' and 'q' are compared. Obviously, 'q' is larger, so - 1 is returned
4.1 strncmp function
Similar to strncpy, it compares the first num characters of two strings. The rule is the same as that of strcmp function.
4.2 simulation implementation of strcmp
int my_strcmp(const char* a, const char* b) { assert(a && b); while (*a == *b) { if (*b == '\0') return 0; a++; b++; } /*if (*a > *b) return 1; if (*a < *b) return -1;*/ return *a - *b;//This step is in place }
5. strstr (returns the pointer of the first occurrence of str1 at str2)
Is the pointer that returns the position where the b string first appears in a, and am a student will be printed as follows
Returns a null pointer if not found.
int main() { char a[] = "I am a student."; char b[] = "am"; printf("%s", strstr(a, b)); return 0; }
5.1 simulation implementation str
char* my_strstr(const char* str1, const char* str2) { assert(str1 && str2); char* s1; char* s2; char* cp = str1; if (*str2 == '\0')//If '' is passed in return str1; while (*cp) { s1 = cp; s2 = str2; while (*s1 == *s2 && *s1 && *s2) { s1++; s2++; } if (*s2 == '\0') { return cp; } cp++; } return NULL; }
6. strtok (split string)
This function can segment the string as we need. sep is the segmentation flag and str is the segmented string.
Here is an example:
int main() { char a[] = "123@qq.com"; char b[] = "@."; printf("%s\n", strtok(a, b));//Split 123 printf("%s\n", strtok(NULL, b));//Split qq printf("%s\n", strtok(NULL, b));//Split com return 0; }
After the first segmentation, this function will remember the segmentation position and change the segmentation flag of the objective function to '\ 0'
So this function will modify the string. Be careful.
The next time you use it, you only need to pass in a null pointer, which is the focus of this function.
We can also use loops to express its results:
int main() { char *p = "666@qq.com"; const char* sep = ".@"; char arr[30]; char *str = NULL; strcpy(arr, p);//Copy the data and process the contents of the arr array for(str=strtok(arr, sep); str != NULL; str=strtok(NULL, sep)) { printf("%s\n", str); } }
Thank you for seeing this. Give me a compliment if it helps.