C common string functions

Source: Internet
Author: User

There are a lot of operations on strings at ordinary times. Understanding common string functions can make C Programming very fast! Here we will sort it out to facilitate future reference. During use, a large number of pointer operations are used. Note that the header file is added:

 
# Include <String. H>

 

I. Str Series

1. strtok

 
Extern Char* Strtok (Char* S,Const Char* Delim );

Function: Splits a string into a group of tag strings. S is the string to be decomposed, and delim is the separator string.

Note:Strtok () is used to split strings into segments. When the delim Delimiter is found in the string of parameter S in strtok (), the character is changed to \ 0. In the first call, strtok () must be given the parameter S string. In future calls, the parameter S is set to null. If each call is successful, the pointer to the split part is returned.If no split string exists, null is returned.All delim characters are filtered out, And the filtered characters are set as a separate node.

Example:

 /*  Strtok example */  # Include <Stdio. h> # Include < String . H> Int Main ( Void  ){  Char STR [] = "  -This, a sample string.  "  ;  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  ;} 
 
SplittingString "-This, a sample string."Into tokens: thisasampleStringThe end:-This

Note:The strtok function destroys the integrity of the string to be decomposed, and the s before and after the call is different. In addition, it seems that the tab \ t cannot serve as a delimiter.

2. strstr

 
Char* Strstr (Const Char* Str1,Const Char* Str2 );

Function: locate the first occurrence of str2 from str1 (do not compare the terminator null). If it is not found, return null.

Example:

 /*  Strstr example  */  # Include <Stdio. h> # Include < String . H> Int  Main (){  Char STR [] = "  This is a sIMple string  "  ;  Char * PCH; PCH = Strstr (STR, "  SIMple  "  ); Strncpy (PCH,  "  SAMple  " , 6 ); Puts (PCH); puts (STR );  Return   0  ;} 
 
SampleStringThisIsA sampleString

3. strchr

 
Char* Strchr (Const Char* STR,IntCh );

Function: finds the location of the first occurrence of the CH in the STR string.
Note: return the pointer to the location where ch occurs for the first time. If ch does not exist in STR, return null.

Example:

 /*  Strchr example  */ # Include <Stdio. h> # Include < String . H> Int  Main (){  Char STR [] = "  This is a simple string  "  ;  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  ;} 
LookingForThe'S'CharacterIn "This is a simple string"... Found4Thfound7Thfound11Thfound18Th

4. strcpy

 
Char* Strcpy (Char* DEST,Const Char* SRC );

Function: Copies the string ending with null indicated by Src to the array indicated by DeST.
Note: The memory areas specified by Src and DEST cannot overlap and DEST must have sufficient space to accommodate SRC strings. Returns the pointer pointing to the end of the Dest character (null.

Similar:

Strncpy

 
Char* Strncpy (Char* DEST,Const Char* SRC, size_t num );

Stpcpy

Non-library functions are used exactly the same as strcpy

5. strcat

 
Char* Strcat (Char* DEST,Const Char* SRC );

Function: add the SRC string to the end of DeST (overwrite '\ 0' at the end of DEST) and add' \ 0 '.
Note: The memory areas specified by Src and DEST cannot overlap and DEST must have sufficient space to accommodate SRC strings.
Returns the pointer to DeST.

Similar strncat

Char* Strncat (Char* DEST,Const Char* SRC, size_t num );

6. strcmp

 
IntStrcmp (Const Char* Str1,Const Char* Str2 );

Function: Compares str1 and str2 strings.
Note:
When S1 <S2, return value <0
When S1 = S2, the return value is 0.
When S1> S2, return value> 0

Similar:

Strncmp

 
IntStrncmp (Const Char* Str1,Const Char* Str2, size_t num );

Strcasecmp

Extern IntStrcasecmp (Const Char* Str1,Const Char* Str2 );

Strncasecmp

 
Extern IntStrncasecmp (Const Char* Str1,Const Char* Str2, size_t num );

7. strcspns

 
Size_t strcspn (Const Char* Str1,Const Char* Str2 );

Function: search for any character in S2 in string S1.
Description: return the number of characters that have been read when the characters in S2 appear, that is, the length of the substring that does not appear in S1.

 /*  Strcspexample  */  # 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 strspns (Returns the length of the initial portionStr1Which consists only of characters that are partStr2.)

 
Size_t strspns (Const Char* Str1,Const Char* Str2 );
# Include <stdio. h> # Include < String . H> Int  Main (){  Char STR [] ="  1589fcba73  "  ;  Char Keys [] = "  1234567890  "  ;  Int I = Strspns (STR, keys); printf (  "  The Beginning % d characters are all in keys \ n  "  , I );  Return  0  ;} 
 
The beginning4Characters are allInKeys

8. strlen

 
Size_t strlen (Const Char* Str );

Function: calculates the length of a string 'str '.
Description: return the length of STR, excluding the terminator null. (Note the difference with sizeof)

Similar strnlen

 
Size_t strnlen (Const Char* STR, size_t maxlen );

9. strdup

 
Extern Char* Strdup (Char* Str );

Function: copy the STR string.
Note: The pointer to the copied string is returned. The space required is allocated by malloc () and can be released by free.

# Include <stdio. h># Include<String. H>IntMain (){Char* STR ="1234567890";Char* P =Strdup (STR); printf ("The duplicated string is: % s \ n", P );Return 0;}

 

Ii. mem Series

1. memset

Void* Memset (Void* PTR,IntValue, size_t num );

Function: sets the first num bytes of the memory area referred to by PTR to the character value.
Returns the pointer to PTR. It can be used for variable initialization and other operations.

Example:

 # include 
   
   #include  
    string . h> 
    int  
    main () { 
    char  STR [] = 
   "  
    almost erery programer shold 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: Copy num bytes from the memory area indicated by Src to the memory area indicated by DeST.
Description: memory regions referred to by Src and destOverlappingBut the copied SRC content will be changed. The function returns a pointer to DeST.

Example:

 # include 
   
   #include  
    string . h> 
    int  
    main () { 
    char  STR [] = 
   "  
    memmove can be very useful ......  
    " 
   ; memmove (STR  + 
    20 , STR + 
    15 , 
    11  
    ); printf ( 
    " 
    the STR is: % s \ n  
    " 
   , str);  
    return  
    0  
   ;} 
  
The STRIs: Memmove can be very useful.

3. memcpy

 
Void* Memcpy (Void* Destination,Const Void* Source, size_t num );

Similar to strncpy. Difference: Copy memory data of the specified size regardless of the content (not limited to strings ).

4. memcmp

 
IntMemcmp (Const Void* Ptr1,Const Void* Ptr2, size_t num );

Similar to strncmp

5. memchr

Void* Memchr (Const Void* Buf,Int ch, Size_t count );

Function: searches for the character ch from the first count bytes of the memory area specified by BUF.
Note: When the first ch character is encountered, stop searching. If the call succeeds, a pointer to the CH character is returned; otherwise, null is returned.

 

 

Organized from: http://www.cplusplus.com/reference/clibrary/cstring/size_t/ (English) http://www.kuqin.com/clib/string/memchr.html (more errors)

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.