ps: This is the first time I use this thing to record the knowledge points that I am easy to confuse. It may not be so refined, or some small errors (please point out), forgive me! Let's start directly~
One, the length of the string
1.strlen function
- Header file: string.h
- Format: strlen (string address)
- Return value: return the actual length of the string, excluding'\0'
char str[]="01234";
printf("%d\n",strlen(str));
Result output: 5
Second, the copy of the string
1.strcpy function
- Header file: string.h
- Format: strcpy (character array, string)
- Return value: the value of the first parameter, that is, the address of a character
- Note: 1>The character array must be large enough
2>When copying,'\0' is copied together
3>The assignment statement cannot be used to assign a character array
4>The copied string is called the target string, and the original string is called The source string
5> can copy the entire string or a part of the array
A.
char str1[10],str2[10];
scanf("%s",str2);
strcpy(str1,str2):
B.
const char * orig="beast";
char copy[40]="Be the best that you can be.";
char *ps;
ps=strcpy(copy+7,orig);
puts(copy);
Result output: Be the beast
2.strncpy function
- Header file: string.h
- Format: strncpy (character array, string, length)
- Return value: return the first address of the character array
- Note: The character array must be large enough
- Function: Copy the first n characters of the string to the character array, and add'\0' to the end
char str[40];
strncpy(str,"0123456789",5);
printf("%s\n",str);
Result output: 01234
Three, string comparison
1.strcmp function
- Header file: string.h
- Format: strcmp (string 1, string 2)
- Comparison rule: compare two strings character by character from left to right (ASCII code) until they encounter different characters or'\0'
- Return value: return int type integer, if string 1<string 2, then return a negative number
printf("%d\n",strcmp("apples","apple"));
printf("%d\n",strcmp("Z","a"));
Result output: 1 -1
2.strncmp function
- Header file: string.h
- Format: strncmp (string 1, string 2, length)
- Return value: return int type integer, if string 1<string 2, then return a negative number
- Function: Compare the substring of the first n characters of string 1 with the substring of the first n characters of string 2
printf("%d\n",strncmp("abcd","abcDEF",3));
printf("%d\n",strncmp("abcd","abcDEF",5));
Result output: 0 1 (greater than 0)
3.strnicmp function
- Header file: string.h
- Format: strnicmp (string 1, string 2, length)
- Difference: strnicmp is not case sensitive when comparing two strings, while strcmp is case sensitive
printf("%d\n",strcmp("A","a"));
printf("%d\n",strnicmp("A","a",1));
Result output: -1 (less than 0) 0
Fourth, the splicing of strings
1.strcat function
- Header file: string.h
- Format: strcat (string 1, string 2)
- Return value: return the first parameter, that is, the address of the first string after splicing the second string
char str[40]="wonderflower";
char str1[]="s smell like old shoes.";
strcat(str,str1);
puts(str);
Result output: wonderflowers smell like old shoes.
2.strncat function
- Header file: string.h
- Format: strncat (string 1, string 2, maximum number of characters added)
char str[40]="wonderflower";
char str1[]="s smell like old shoes.";
strncat(str,str1,7);
puts(str);
Result output: wonderflowers smell
Five, the combination of strings
1.sprintf function
- Header file: stdio.h
- Format: sprintf (address of target string, format string and list of items to be written)
- Function: Combine multiple elements into a string
char str[40]="hello world!";
int year=2020;
char final[40];
sprintf(final, "%s %d\n",str,year);
puts(final);
Result output: hello world! 2020
Six, other string functions
1. The strchr function
char *strchr(const char * s, int c);
if the s string contains c characters, this function returns the c pointer that points to the first occurrence of the s string (the null character at the end is also part of the string, so Within the search range); if the c character is not found in the string s, the function returns a null pointer.
2.strpbrk function
char *strpbrk(const char * s1, const char * s2);
If the s1 character contains any character in the s2 string, the function returns a pointer to the first position of the s1 string; if it is in the s1 string If no character in the s2 string is found, a null character is returned.
3. The strrchr function
char *strrchr(const char * s, int c);
This function returns the last position of the c character in the s string (the empty character at the end is also part of the string, so it is in the search range). If the c character is not found, a null pointer is returned.
char str[100] = "hello worldfor2021";
char s = 'o';
char s1 = 'a';
char *n;
n = strrchr(str,s);
printf("%s\n",n); //输出or2021
n = strrchr(str,s1);
printf("%s",n); //输出(null)
4.strstr function
char *strstr(const char *s1, const char * s2);
This function returns the first position of the s2 string in the s1 string. If s2 is not found in s1, a null character is returned.
char str[100] = "hello worldfor2021";
char s[100] = "for20";
char s1[100] = "hey";
char *n;
n = strstr(str,s);
printf("%s\n",n); //输出for2021
n = strstr(str,s1);
printf("%s",n); //输出(null)
- Judge the pointer is empty
if(!n)
printf("1\n");
- Return subscript
int num;
num = n - str;
printf("%d\n",num);