Reprinted Source: https://blog.csdn.net/qq_33757398/article/details/81212618
String related operations header files: string.h
1.strcpy function
Prototype: strcpy (str1, str2) ;
Function: string str2 copied into the string in str1, str1 and covers the original string, the string can be used to assign values to variables
Return: str1
note:
1) will cover all of the characters in str2 in str1,
2) can not exceed the length of the string str2 str1,
3) copies of works: from the beginning of the first element encountered \ 0 End
int main ( int argc, char const * the argv []) { char * str1 = " Hello World " ; char * str2; // Function: copy the contents of str1 to str2, character parameter array pointer strcpy (str2, str1) ; printf ( " str2 =% S \ the n- " , str2); char str3 [ 15 ] = " the Hello \ 0 world " ; char str4 [ 15 ]; // copy principle: start from the first element encountered \ 0 end strcpy (str4, Str3); the printf ( " str4 S =% \ n-", str4); return 0; }
Export
str2 = hello world
str4 = hello
2.strncpy function
Prototype: strncpy (str1, str2, n-);
Function: a character string in str2 n characters copied to the first n characters of a string str1
Return: str1
note:
1) does not clear all the strings in str1, only changes the first n strings,
2) n is not greater than the string str1, str2 of length
3) But if you use strncpy_s will remove all the strings in str1
int main(int argc, char const *argv[]) { 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
Prototype: strcat (str1, str2) ;
Function: string str2 added to the end of the string str1, ie splicing two strings
Return: str1
int main(int argc, char const *argv[]) { char str1[] = "hello "; char str2[] = "world"; strcat(str1, str2); // hello world printf("%s\n", str1); return 0; }
4.strncat function
Prototype: strncat (str1, str2, n-) ;
Function: the first n characters in str2 is added to the end of the string str1
Return: str1
Note: after the splice length can not exceed the length of the string array str1
int main(int argc, char const *argv[]) { char str1[] = "hello "; char str2[] = "world"; strncat(str1, str2, 2); // hello wo printf("%s\n", str1); return 0; }
5.strlen function
Prototype: strlen (str1) ;
Function: Compute the str1 string length
Returns: an int
Note: the length of the strings does not include the character '\ 0'
{ 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
Prototype: strcmp (str1, str2) ;
Function: Comparison of two strings, if the strings are equal, then return 0; if str1 str2 greater than (greater than for understanding the start means compares the first two characters of the string, if the same two characters, Comparative continued, if two characters are not found to be equal, and the ASCII code of the character in str1 str2 is greater than, greater than said str1 str2), returns a positive number (this is not necessarily a positive number); if less than str2 str1 returns a negative number (not necessarily -1); the same character strings when the length of the previous str1 is greater than str2, str1 and str2 of characters, but also with respect to the processing str1 is greater than str2
Prototype 2: a strncmp (str1, str2, n-) ;
Function 2: compare the first n characters of two strings
Prototype 3: stricmp (str1, str2) ; (stricmp use in Windows, use strcasecmp in Linux)
Function 3: Ignore compare two strings in the case of a string , that is not case sensitive
Note: If you use directly in the VS2017 in stricmp will prompt the following error:
Cause and see approach: stricmp wrong , that instead of using _stricmp
Returns: 0 or a positive or a negative number
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
7.strchr function
Prototype: the strchr (STR, C) ;
Function: str string to find the first occurrence of the character c (Find from the first address of the string)
Prototype 2: The strrchr (STR, C) ;
Function 2: In the string str from the start looking forward position of the first occurrence of the character c
Prototype. 3: Strstr (str1, str2);
Function 3: string search string str2 in str1 position, if found, the pointer position of the first character in str2 in str1 is returned, if not found, returns NULL
Returns: pointer to the location of the character c If character c is not found, it returns a null pointer NULL
{ // strchr query string and query string strstr function char * str = " NO One CAN Help you " ; // Query h in a string of characters, if present, the return address h // If not, return NULL char the strchr = S * (STR, ' H ' ); IF (S) { the printf ( " character exists:% C \ n " , * S); } the else { the printf ( " not character \ n exists " ); } // If so, return to the position where the first string address // otherwise it returns NULL char * ST = strstr (str, "Help " ); IF (ST) { printf ( " there is a string: the p-% \ the n- " , * ST); // Note: here they are returned string where the first address, which is an array of strings, so do not take address value * ST // as (number of characters crude) takes on a value of the address string * buf is of no use, it has always been a pointer to take // be the value of the pointer, the pointer points to a value not // on the type of pointer variables: & a fetch address value (pointer variable) is the value of the address is located, * st represents the value of the pointer variable points, and when defined as // these two operations is exactly the opposite of } the else { the printf ( " not in the string \ n- " ); } return 0 ; }
8.strpbrk function
Prototype: The strpbrk (str1, str2) ;
Function: sequentially testing str1 string of characters, when the test character string str2, also contains , the test is stopped, and returns the character position
Returns: a first pointer position two character strings are included in the str1
char str1[] = "We12are34csdn!"; char str2[] = "32"; char* ret1; ret1 = strpbrk(str1, str2); //*ret1 = 2 int r1 = ret1 - str1; //r1 = 3 printf("%c\n", *ret1); printf("%d\n", r1);
9. The string-to-digital atoi, atof, atol function
atoi (str); // int Integer String to convert
atof (str); // Converts a string to a floating-point double
atol (str); // convert to string long plastic
{ // function 1, turn integer string, atoi () // Note: scanning string preceding space skipped until a numeral or sign conversion is started, or non-numeric encountered \ 0 End Conversion the return value conversion // source: stdlib.h header file // extensions: there are similar atof, atol name suggests, is to turn float, Long // Note: You must own a string of numbers that can be converted correctly // example: char * STR = " 125 " ; int I = atoi (STR); the printf ( " I D =% \ n- " , I); // digital-to-string int J = 10005 ; char ARR [ 20 is ]; sprintf (ARR , " % d ", J); // C + language which can not be directly connected, so the need to format the printf ( " ARR S =% \ n- " , ARR); return 0 ; }