Usually the operation of the string is a lot of, understand the common string function will make C programming very fast! Here properly tidy up, convenient for later reference. When used, a large number of pointers are used, note the header file:
#include <string.h>
First, STR series
1.strtok
extern Char *strtok (char *s, const char *delim);
Function: Decomposes a string into a set of tag strings. S is the string to be decomposed, Delim is the delimiter string.
Description: Strtok () is used to split a string into fragments. When Strtok () finds the split character of the parameter Delim in the string of the parameter s, the character is changed to the \ s character. At the first call, Strtok () must give the parameter S string, and the subsequent call sets the parameter s to null. Each successful call returns a pointer to the fragment being split. Null is returned when there is no split string. All the characters contained in the Delim will be filtered out and set to a segmented node where the filter is removed.
Example:
/* Strtok Example */#include <stdio.h> #include <string.h>int main (void) { char str[] = "-this, a sample s Tring. "; char *pch; printf ("Splitting string \"%s\ "into tokens:\n", str); PCH = Strtok (str, ",.-"); while (pch! = NULL) { printf ("%s\n", PCH); PCH = Strtok (NULL, ",.-"); } printf ("At the End:%s", str); return 0;}
Splitting string "-this, a sample string." To Tokens:thisasamplestringthe end:-This
Note: The Strtok function destroys the integrity of the decomposed string, which is different before and after the call. In addition, the seemingly tab \ t cannot function as a split character.
2.strstr
char * STRSTR (const char * str1, const char * str2);
Function: Looks for the first occurrence of str2 from the string str1 (does not compare terminator null) and returns NULL if not found.
Example:
/* STRSTR Example */#include <stdio.h> #include <string.h>int main () { char str[] = "This is a sim ple string "; char *pch; PCH = Strstr (str, "sImple"); strncpy (PCH, "sample", 6); Puts (PCH); Puts (str); return 0;}
Sample Stringthis is a sample string
3.strchr
char * STRCHR (const char *STR, int ch);
Function: Find the position of the first occurrence of the character ch in the string str
Note: Returns a pointer to the position of the first occurrence of CH, or null if no ch is present in Str.
Example:
/* STRCHR Example */#include <stdio.h> #include <string.h>int main () { char str[] = "This was a simple strin G "; char *pch; printf ("Looking for the ' s ' character in \"%s\ "... \ n", str); PCH = STRCHR (str, ' s '); while (pch! = NULL) { printf ("found at%d th\n", Pch-str + 1); PCH = STRCHR (pch + 1, ' s '); } return 0;}
Looking for the ' s's ' character in ' a simple string ... found at 4 thfound at 7 Thfound at one thfound at th
4.strcpy
char * strcpy (char * dest, const char * src);
function: Copy the null-terminated string from SRC to the array referred to by Dest.
Description: The memory areas referred to by SRC and dest cannot overlap and dest must have sufficient space to accommodate the SRC string. Returns a pointer to the character (NULL) at the end of the dest.
Similar to:
strncpy
char * strncpy (char * dest, const char * src, size_t num);
stpcpy
Non-library functions, use exactly the same as strcpy
5.strcat
char * strcat (char * dest, const char * src);
Function: Add the string of SRC to the end of the dest ("\" At the end of the dest) and add '/'.
Description: The memory areas referred to by SRC and dest cannot overlap and dest must have sufficient space to accommodate the SRC string.
Returns a pointer to the dest.
Similar to Strncat
char * STRNCAT (char * dest, const char * src, size_t num);
6.strcmp
int strcmp (const char * str1, const char * str2);
Function: Compare strings str1 and str2.
Description
When S1<s2, the return value <0
When S1=s2, the return value =0
When S1>s2, the return value >0
Similar to:
strncmp
int strncmp (const char * str1, const char * str2, size_t num);
strcasecmp
extern int strcasecmp (const char *STR1, const char *STR2);
strncasecmp
extern int strncasecmp (const char *STR1, const char *STR2, size_t num);
7.strcspn
size_t strcspn (const char * str1, const char * str2);
Function: Searches the string S1 for any of the characters that appear in the S2.
Description: Returns the number of characters that have been read when a character in S2 is present, that is, the length of a substring that appears in S1 and does not appear in S2.
/* STRCSPN Example */#include <stdio.h> #include <string.h>int main () { char str[] = "fcba73"; Char keys[] = "1234567890"; int i = STRCSPN (str, keys); printf ("Already read%d characters\n", i); printf ("The first number in both STR and keys is%d th\n", i + 1); return 0;}
Similar strspn (Returns the length of the initial portion of str1 which consists only of characters that is part of str2.)
size_t strspn (const char * str1, const char * str2);
#include <stdio.h> #include <string.h>int main () { char str[] = "1589fcba73"; Char keys[] = "1234567890"; int i = STRSPN (str, keys); printf ("The beginning%d characters is all in keys \ n", i); return 0;}
8.strlen
size_t strlen (const char * str);
function: Calculate the length of the string str
Description: Returns the length of STR, excluding the Terminator null. (Note the difference from sizeof)
Similar to Strnlen
size_t strnlen (const char *STR, size_t maxlen);
9.strdup
extern Char *strdup (char *str);
Function: Copy string str
Description: Returns a pointer to the copied string that is allocated by malloc () and can be freed by free ().
#include <stdio.h> #include <string.h>int main () { char *str = "1234567890"; Char *p = strdup (str); printf ("The duplicated string is:%s\n", p); return 0;}
Second, MEM series
1.memset
void * memset (void * ptr, int value, size_t num);
Function: Sets the first num bytes of the memory area referred to as PTR to a character value.
Description: Returns a pointer to PTR. Can be used for operations such as variable initialization
Example:
#include <stdio.h> #include <string.h>int main () { char str[] = "Almost erery programer should know memset! "; memset (str, '-', sizeof (str)); printf ("The STR is:%s now\n", str); return 0;}
2.memmove
void * Memmove (void * dest, const void * src, size_t num);
Function: Copies num bytes to the memory area referred to by Dest by the memory area referred to by SRC.
Description: The memory areas referred to by SRC and dest can overlap , but the SRC content will be changed after copying. The function returns a pointer to Dest.
Example:
#include <stdio.h> #include <string.h>int main () { char str[] = "memmove can be very useful ..."; Memmove (str + +, str + +); printf ("The STR is:%s\n", str); return 0;}
The Str is:memmove can be very very useful.
3.memcpy
void * memcpy (void * destination, const void * source, size_t num);
Similar to strncpy. Difference: Copies the memory data of the specified size regardless of the content (not limited to strings).
4.memcmp
int memcmp (const void * PTR1, const void * ptr2, size_t num);
Similar strncmp
5.memchr
Function: Finds the character Ch from the first count byte of the memory area referred to by BUF.
Description: Stops searching when the character Ch is encountered for the first time. Returns a pointer to the character CH if successful, otherwise returns NULL.
Whole article excerpted from: http://www.cnblogs.com/xiangzi888/archive/2012/04/16/2451947.html
C Common String functions