1. strcpy function
Syntax: strcpy(str1,str2)
Function: Copy the string str2 to the string str1, and override the STR1 original string, which can be used to assign values to string variables.
Return: str1
Be careful:
(1). The string str2 will cover all the characters in str 1;
(2) The length of the string STR 2 cannot exceed str1;
(3) Copy principle: Start with the first element and end with\0.
int main() { char *str1 = "hello world"; char *str2; // Copy the contents of str1 to str2, parameter is character array pointer strcpy(str2, str1); printf("str2 = %s\n", str2); // str2 = hello world char str3[15] = "hello\0 world"; char str4[15]; // The contents of str4 are copied to str3 and end with\0 strcpy(str4, str3); printf("str4 = %s\n", str4); // str4 = hello return 0; }
2. strncpy function
Syntax: strncpy(str1,str2,n)
Function: Copy the first n characters of string str2 into the first n characters of string str1.
Return: str1
Be careful:
(1). Not all strings in str 1 will be cleared, only the first n strings will be changed;
(2). n cannot be greater than the length of strings str1 and str 2;
(3). But if strncpy_is used S will clear all strings in str1.
int main() { char str1[] = "day day up"; char str2[] = "you are"; strncpy(str1, str2, strlen(str2)); printf("%s\n", str1); // you are up return 0; }
3. strcat function
Syntax: strcat(str1,str2)
Function: Add the string str2 to the end of the string str1, that is, stitch two strings together.
Return: str1
int main() { char str1[] = "hello"; char str2[] = "world"; strcat(str1, str2); printf("%s\n", str1); // hello world return 0; }
4. strncat function
Syntax: strncat(str1,str2,n)
Function: Add the first n characters of the string str2 to the end of the string str1.
Return: str1
Note: The length after stitching cannot exceed the length of the string array str1.
int main() { char str1[] = "hello "; char str2[] = "world"; strncat(str1, str2, 2); printf("%s\n", str1); // hello wo return 0; }
5. strlen function
Syntax: strlen(str1)
Function: Calculates the length of the string str1.
Return: int value
Note: The length of the string does not include the character'\0'.
int main() { char *str1 = "hello world"; char *str2 = "hello,\0 kite"; int l1 = strlen(str1); int l2 = strlen(str2); printf("l1 = %d\n", l1); // 11 printf("l2 = %d\n", l2); // 6 return 0; }
6. strcmp function
(1). Syntax: strcmp(str1,str2)
Function: Compare two strings.
A. Returns 0 if the two strings are equal;
B. If str1 is greater than str2, return a positive number, which is not necessarily 1;
C. If str1 is less than str2, a negative number is returned, which is not necessarily -1;
(2). Syntax: strncmp(str1,str2,n)
Function: Compare the first n characters of two strings.
(3). Syntax: stricmp(str1,str2) (stricmp in Windows and strcasecmp in Linux)
Function: Ignore case comparison strings in two strings, that is, insensitive to case.
Note: If stricmp is used directly in VS2017, the following error will be prompted.
Processing available _ stricmp instead.
Return: 0 or a positive or negative number.
int main() { char str1[] = "Wearecsdn!"; char str2[] = "Wearecsdn!"; char str3[] = "Wearea!"; char str4[] = "Wearef!"; char str5[] = "Weare"; char str6[] = "weAreCsdn!"; int cmp1 = strcmp(str1, str2); //cmp1=0 int cmp2 = strcmp(str1, str3); //cmp2=1 int cmp3 = strcmp(str1, str4); //cmp3=-1 int cmp4 = strcmp(str1, str5); //cmp4=1 int cmp5 = strncmp(str1, str2, 5); //cmp5=0 int cmp6 = strncmp(str1, str3, 5); //cmp6=0 int cmp7 = strncmp(str1, str4, 5); //cmp7=0 int cmp8 = strncmp(str1, str5, 5); //cmp8=0 int cmp9 = _stricmp(str1, str6); //cmp9=0 return 0; }
7. strchr function
(1). Syntax: strchr(str,c)
Function: Start with the first address of the string, and find the position of the first character c in the str string.
(2). Syntax: strrchr(str,c)
Function: Searches for the first occurrence of the character c from the back to the front in the string str.
(3). Syntax: strstr(str1,str2)
Function: Searches for the position of the string str2 in the string str1, returns a pointer to the position of the first character of str2 in STR1 if found, and returns NULL if not found.
Return: A pointer to the position of the character c, or a null pointer NULL if the character c is not found.
int main() { // strchr query character char *str = "no one can help you"; // Query the character h in the string if there is an address that returns H // If no return NULL exists char *s = strchr(str, 'h'); if(s){ printf("Existing characters:%c\n", *s); } else { printf("No characters exist\n"); } // strstr query string function char *st = strstr(str, "help"); // Query the character help in the string, if there is a first address to return help // If no return NULL exists if(st){ printf("There is a string:%p\n", *st); } else { printf("Not in string\n"); } return 0; }
8. strpbrk function
Syntax: strpbrk(str1,str2)
Function: Checks the characters in the string str1 in turn, stops the check and returns the character position when the character being checked is also included in the string str2.
Return: A pointer to the position of the first two characters in str1.
int main() { char str1[] = "We12are34csdn!"; char str2[] = "32"; char* ret1; ret1 = strpbrk(str1, str2); int r1 = ret1 - str1; printf("%c\n", *ret1); // *ret1 = 2 printf("%d\n", r1); // r1 = 3 return 0; }
9. String to Number atoi, atof, atol Functions
(1). Syntax: atoi(str)
Function: Convert string to int integer
(2). Syntax: atof(str)
Function: Convert string to double floating point number
(3). Syntax: atol(str)
Function: Convert string to long integer