String processing functions
1. Write formatted data into a string: sprintf
Int sprintf (char * buffer, const char * format ,...);
Print data to buffer
Example: char result [100];
Int num = 24;
Sprintf (result, "% d", num );
Example: char string [50];
Int file_number = 0;
Sprintf (string, "file. % d", file_number );
File_number ++;
Output_file = fopen (string, "w ");
For example, char result [100];
Float fnum = 3.14159;
Sprintf (result, "% f", fnum );
2 String Length query function: strlen
Int strlen (const char * s );
3. String replication functions: strcpy and strncpy
Char * strcpy (char * dest, const char * src );
4. String concatenation function: strcat
Char * strcat (char * dest, const char * src );
5. String comparison functions: strcmp, strncmp, stricmp, and strnicmp
String comparison function strcmp (case sensitive)
Int strcmp (const char * s1, const char * s2 );
Return Value
Return value
Explanation
Less than 0
Str1 is less than str2
Equal to 0
Str1 is equal to str2
Greater than 0
Str1 is greater than str2
String search functions: strcspns, strspns, strstr, strtok, strchr
6. Search for the substring strstr.
Char * strstr (char * s1, char * s2 );
Returns the position pointer for the first occurrence of a given string.
If this parameter is found, a pointer is returned, pointing to the position where s2 appears for the first time in s1.
If no value is found, NULL is returned.
Pdest = strstr (string, str );
Search for 'str' in 'string' and return the position where 'str' appears for the first time in 'string '.
For example: char * str1 = "this is a string of characters ";
Char * str2 = "a string ";
Char * result = strstr (str1, str2 );
If (result = NULL) printf ("cocould not find % s in % s", str2, str1 );
Else printf ("Found a substring: % s", result );
Output result: Found a substring: a string of characters
7. Search for the position strchr where the character first appears in the string
Pdest = strchr (string, ch );
Search for ch in string and return the position where str appears for the first time in string.
8. Copy strncpy from the string, and copy all strcpy from the string.
Char * strncpy (char * dest, char * src, int maxlen );
Char * strcpy (char * dest, char * src );
Copy the previous maxlen characters from src to dest
1) if the src contains less than maxlen characters, it will be copied together ''.
2) If src contains more than or equal to maxlen characters, copy maxlen characters
9 string case-insensitive conversion functions: strlwr, strupr
Supplement: These functions require # include <string. h>