String processing functions in C Language

Source: Internet
Author: User
Tags strtok

1. Compare string size functions

1) Case Insensitive --- strcasecmp

Function prototype: Int strcasecmp (const char * S1, const char * S2 );

Function Description: Used to compare S1 and S2 strings. Differences in Case sensitivity are automatically ignored during comparison.

2) case-insensitive-stricmp

Function prototype: Int stricmp (char * str1, char * str2 );

Function Description: Compares two strings in upper and lower case insensitive mode.

3) case-insensitive-strcmp

Function prototype: Int strcmp (char * str1, char * str2 );

Function Description: Compares str1 and str2 strings by comparing the ASCII codes of each character in a string. The case sensitivity of the characters is considered during comparison.

4) Comparison part-strncmpi

Function prototype: Int strncmpi (char * str1, char * str2, unsigned maxlen );

Function Description: Compares the first maxlen characters of str1 and str2.

5) memory region comparison --- memcmp

Function prototype: Int memcmp (void * buf1, void * buf2, unsigned int count)

Function Description: Compare the first count bytes of buf1 and buf2 in the memory area. Void * refers to any type of pointer.

6) memory area comparison-memicmpVoid * refers to any type of pointer.

Function prototype: Int memicmp (void * buf1, void * buf2, unsigned int count)

Function Description: Compare the first count bytes of buf1 and buf2 in the memory area, but the values are case-insensitive.

 

Return values of the above comparison functions: If the string in parameter 1 is the same as the string in parameter 2, 0 is returned;

If the string length in parameter 1 is greater than the string length in parameter 2, a value greater than 0 is returned;

If the string length in parameter 1 is smaller than the string length in parameter 2, a value smaller than 0 is returned.2. Extract substrings from strings

1) extract the substring -- strstr

Function prototype: Char * strstr (char * SRC, char * Find)

Function Description: Search for the position where find first appears from the SRC string (not compare the terminator null)

Return Value: Returns the pointer pointing to the first find position. If not found, null is returned.

2) extract the string between separators-strtok

Function prototype: Char * strtok (char * SRC, char * delim );

Function Description: The string to be decomposed. SRC is the string to be decomposed, and delim is the separator string.

For the first call, Src must point to the string to be decomposed, and then calls to set S to NULL;

In strtok, Src searches for characters contained in delim and replaces them with null ('\ 0') until the entire string is searched.

Return Value: Split strings starting with S. 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:

# Include <string. h>

# Include <stdio. h>

Int main (){

Char * s = "golden Global View ";

Char * D = "";

Char * P;

P = strtok (S, d );

While (p ){

Printf ("% s \ n", P );

Strtok (null, d );

}

Return 0;

}

Output: Golden

Global

View

3. Copy strings

1) string replication -- strcpy

Function prototype: Char * strcpy (char * DEST, char * SRC)

Function Description: Copy the string ending with null indicated by Src to the array indicated by DeST.

The memory areas caused by Src and DEST cannot overlap and DEST must have enough space to hold SRC strings.

Return Value: Returns the pointer to DeST.

2) string replication -- strdup

Function prototype: Char * strdup (char * SRC)

Function Description: Copy the SRC string

Return Value: Returns the pointer to the copied string. The space required is allocated by malloc () and free () can be released.

3) memory space replication-memcpy

Function prototype: Void * memcpy (void * DEST, void * SRC, unsigned int count );

Function Description: The memory areas specified by Src and DEST cannot overlap. The memory areas caused by Src copy count bytes to the memory area specified by DeST.

Return Value: Returns the pointer to DeST.

4. String connection

1) end-to-end connection -- strcat

Function prototype: Char * strcat (char * DEST, char * SRC)

Function Description: Add the string indicated by Src to the end of DeST (overwrite '\ 0' at the end of DEST) and add' \ 0'

2) partially connected -- strncat

Function prototype:Char * strncat (char * DEST, char * SRC, int N );

Function Description: Add the first n characters of the string in SRC to the end of DeST (overwrite '\ 0' at the end of DEST) and add ''\ 0 '.

Return Value: Returns the pointer to DeST.

5. Search for characters from strings

1) Search for characters in the memory area-memchr

Function prototype: Void * memchr (void * Buf, char CH, usigned count)

Function Description: Searches for the character ch from the first count bytes of the memory area indicated by BUF. If the character ch is encountered for the first time, the query is stopped.

Return Value: If yes, the pointer pointing to the CH character is returned; otherwise, null is returned.

2) Find the character in the string -- strchr

Function prototype: Char * strchr (char * SRC, char ch)

Function Description: Search for the location where the character ch appears for the first time in string S.

Return Value: Returns the pointer to the position where C appears for the first time. If C does not exist in S, null is returned.

3) Search for the current character-strcspns

Function prototype: Int strcspn (char * SRC, char * Find)

Function Description: Search for characters in find in the SRC string

Return Value: Return the next mark value of the first occurrence character in SRC, that is, the length of the string that appears in SRC but not in find.

Example:

# Include <string. h>

# Include <stdio. h>

Int main (){

Char * s = "golden Global View ";

Char * r = "new ";

Int N;

N = strcspn (S, R );

Printf ("the first char both in S1 and S2 is: % C", s [N]);

Return 0;

}

Output: The first char both in S1 and S2 is: E

4) match any character -- strpbrk

Function prototype: char * strpbrk (char * S1, char * S2)

Function Description: searches for the position of the first character matching any character in string S2 in string S1. null characters are not included.

Return Value: returns the pointer to the first matched character in S1. If no matched character exists, a null pointer is returned.

Example:

# Include <stdio. h>

# Include <string. h>

Int main (){

Char * S1 = "Welcome to Beijing ";

Char * S2 = "bit ";

Char * P;

P = strpbrk (S1, S2 );

If (P)

Printf ("% s \ n", P );

Else printf ("not found! \ N ");

Return 0;

}

Output: to Beijing

6. Other functions

1) convert all to uppercase --- strupr

Function prototype: Char * strupr (char * SRC)

Function Description: Converts a string SRC to a large write type. Only lowercase letters in SRC are converted without changing other characters.

Return Value: Returns a pointer to SRC.

2) convert all to lowercase --- strlwr

Function prototype: Char * strlwr (char * SRC)

Function Description: Converts a string SRC to lowercase letters. Only uppercase letters in SRC are converted without changing other characters.

Return Value: Returns a pointer to SRC.

3) Reverse the string-strrev

Function prototype: Char * strrev (char * SRC)

Function Description: Reverse the order of all characters in the SRC string (excluding null)

Return Value: Returns the string pointer pointing to the reverse order.

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.