Table of contents
5. Length-limited string length
Use the string function to include the header file #include<string.h>!
1.strlen
- Definition: Calculate the length of the character before '\0' in the array.
- Error-prone: Everyone must be familiar with strlen, but it should be noted that its return value is an unsigned integer (example).
- Simulation implementation
1.1 YiShu (example)
#include<stdio.h>
#include<string.h>
int main()
{
if (strlen("abcd") - strlen("abcdef") > 0)
{
printf(">\n");
}
else
{
printf("<=\n");
}
return 0;
}
According to the logic of this question, the subtraction of the two strlen functions =-3, the result should be <=. However, srtlen returns an unsigned integer, the subtraction of two unsigned integers is still an unsigned integer, then -3 will be a very large integer, and the program result is >. To modify, directly compare the two strlens, or perform mandatory conversion.
1.2 Simulation implementation
1.2.1 Counter form
#include<stdio.h>
#include<assert.h>
int my_strlen(char *str)
{
assert(str!=NULL);
int count = 0;
while (*str != '\0')
{
count++;
str++;
}
return count;
}
int main()
{
char arr[]="abcedf";
printf("%d\n", my_strlen(arr));
return 0;
}
//结果为6。
1.2.2 Pointer-pointer form
#include<stdio.h>
int my_strlen(char* str)
{
char* tmp = str;
while (*str != '\0')
{
str++;
}
return str - tmp;
}
int main()
{
char arr[] = "abcedf";
printf("%d\n", my_strlen(arr));
return 0;
}
//结果为6。
1.2.3 Recursive form
#include<stdio.h>
#include<string.h>
int my_strlen(const char* str)
{
if (*str != NULL)
{
return 1 + my_strlen(++str);
}
else
{
return 0;
}
}
int main()
{
char arr[] = "hello,world!";
printf("%d\n", my_strlen(arr));
return 0;
}
//结果是 12
2.strcpy
- Definition: Copy the contents of the source array to the target array.
- Error-prone: The source string must end with '\0', otherwise it will lead to out-of-bounds and program crash (for example).
- pay attention! : strcpy will copy '\0' of the source string to the target space, the target space must be large enough, and the target space must be variable.
- Simulation implementation
2.1 Easy to use (example)
#include<stdio.h>
int main()
{
char arr1[20] = { 0 };
char arr2[] = { 'a','b','c' };
strcpy(arr1, arr2);
return 0;
}
As can be seen above, the program crashes. It is because arr2 does not have a defined size, strcpy did not find '\0' after copying the content when copying, causing the program to crash.
2.2 Simulation implementation
#include<stdio.h>
#include<assert.h>
char* my_strcpy(char* dest, char* src)
{
assert(dest && src);
char* ret = dest;
while (*dest++ = *src++)
{
;
}
return ret;
}
int main()
{
char arr1[20] = { 0 };
char arr2[] = "abcdef";
printf("%s\n",my_strcpy(arr1,arr2));
return 0;
}
//结果是abcdef
3. broken
- Definition: Appends the contents of the source string into the destination string space. Append from target space '\0'.
- Error-prone: appending yourself will lead to an infinite loop.
- pay attention! : The source and target strings must end with '\0', the target space must be large enough, and the target space must be modifiable.
- Simulation implementation
3.1 Simulation implementation
#include<stdio.h>
#include<assert.h>
char* my_strcat(char* dest, char* src)
{
assert(dest && src);
char* ret = dest;
//让指针指向目标空间的'\0'处
while (*dest)
{
dest++;
}
//在将源字符串内容拷贝到目标空间
while (*dest++ = *src++)
{
;
}
return ret;
}
int main()
{
char arr1[20] = "hello";
char arr2[] = " world";
printf("%s\n", my_strcat(arr1, arr2));
return 0;
}
//结果是 hello world
4.strcmp
- Definition: Compares the size of characters at corresponding positions in two strings until a difference or '\0' is found. When str1>str2, return the number >0; when str1<str2, return the number <0; when str1=str2, return 0.
- Simulation implementation
4.1 Simulation implementation
#include<stdio.h>
#include<assert.h>
int my_strcmp(const char* str1, const char* str2)
{
assert(str1 && str2);
while (*str1 == *str2)
{
if (*str1 == '\0')
{
return 0;
}
str1++;
str2++;
}
if (*str1 > *str2)
{
return 1;
}
else
{
return -1;
}
}
int main()
{
char arr1[] = "abc";
char arr2[] = "abcdef";
int ret = my_strcmp(arr1, arr2);
if (ret > 0)
printf(">");
else if (ret == 0)
printf("=");
else
printf("<");
return 0;
}
//结果是<
5. Length-limited string length
strncpy,strncat,strncmp
You can specify the length to perform corresponding operations, making the code relatively safe
6.strstr
- Definition: Determine whether another string is a substring of the string in a string.
- Simulation implementation
#include<stdio.h>
#include<assert.h>
char* my_strstr(const char* str1, const char* str2)
{
assert(str1 && str2);
const char* s1 = str1;
const char* s2 = str2;
char* cur = str1;
while (*cur)
{
s1 = cur;
s2 = str2;
while (*s1&&*s2&&(*s1 == *s2))
{
s1++;
s2++;
}
if (*s2 == '\0')
{
return cur;
}
cur++;
}
return NULL;
}
int main()
{
char arr1[] = "abcdefgh";
char arr2[] = "cdef";
char* ret = my_strstr(arr1, arr2);
if (ret == NULL)
{
printf("找不到子串\n");
}
else
{
printf("%s\n",ret);
}
return 0;
}
//结果是cdefgh
7.strtok
- Definition: String cutting, given a character set, cutting the content of the source string.
- pay attention! : strtok finds a mark in the string, ends with '\0', and returns a pointer to this mark; strtok will change the manipulated string, it is recommended to copy the content to a new string to ensure the source Strings are not modified. ; When passing a non-NULL pointer, only find the first mark; when passing NULL, search from the saved position backward.
- Use of strtok
7.1 Use of strtok
#include<stdio.h>
#include<string.h>
int main()
{
char arr[] = "hello#world*first";
char sep[] = "#*";
char buf[20];//创建新的字符串
char* str = NULL;
strcpy(buf, arr);
for (str = strtok(buf, sep); str != NULL; str = strtok(NULL, sep))
{
printf("%s\n",str);
}
//上面for循环相当于:
//printf("%s\n",str = strtok(buf, sep));
//printf("%s\n",str = strtok(NULL, sep));
//printf("%s\n",str = strtok(NULL, sep));
//但是这三条语句只对本次arr数组有效,如果arr字符串更长,分割字符集越多就不适用。
return 0;
}
//结果是:
//hello
//world
//first
8th street error
- Definition: Return the error message corresponding to the error code.
- pay attention! : Use this function to include the header file #include<errno.h>; introduce the global variable errno (error code).
- Use of strerror
8.1 Use of strerror
#include<stdio.h>
#include<string.h>
#include<stdlib.h>
int main()
{
int* p = (int*)malloc(INT_MAX);//INT_MAX是一个非常大的数。
if (p == NULL)
{
printf("%s\n",strerror(errno));
//与strerror相同功能,只是不需要调用printf打印,并且可以自定义前缀。
perror("malloc");
return 1;
}
return 0;
}
//结果是
//Not enough space
//malloc: Not enough space
9. Other string functions
Returns true if the argument meets the following conditions
- iscntrl : any control character
- isspace : blank characters: space ' ' , form feed '\f' , line feed '\n' , carriage return '\r' , tab '\t' or vertical tab '\v'
- isdigit : decimal digits 0~9
- isxdigit : hexadecimal digits, including all decimal digits, lowercase letters a f , uppercase letters A F
- islower : lowercase letters a~z
- isupper : uppercase letters A~Z
- isalpha : the letter a z or A Z
- isalnum : letters or numbers, a z, A Z, 0~9
- ispunct : punctuation marks, any graphic characters that are not numbers or letters (printable)
- isgraph : any graphic character
- isprint : any printable character, including graphic characters and whitespace characters
- toupper : Convert characters in a string to uppercase
- tolower : convert the characters in the string to lowercase