C String Processing function--view C language Help documentation

Source: Internet
Author: User
Tags alphabetic character strcmp strtok uppercase character

In C language, usually the operation of the string is a lot of, understand the commonly used string function will make C programming very fast! Here properly tidy up, convenient for later reference. When used, a lot of pointers are used, note the header file: #include <string.h>

First, str series 1.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.

Routines:

int main () {    char str[] = "This was a simple string";    char *pch;        PCH = Strstr (str, "simple"); PCH points to the position of the first character found        puts (PCH);    Puts (str);    return 0;}
Output:
Sample Stringthis is a sample string
2.STRCHR

char * STRCHR (const char *STR, char 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.

Routines:

Char str[] = "This was a simple string"; char *pch;pch = STRCHR (str, ' s '); while (pch! = NULL)
{ printf ("found at%d th\n", Pch-str + 1);  STR is the beginning of the address, the output results from 1 pch = STRCHR (pch + 1, ' s '); Continue looking from the next location}

Output:

Found at 4 thfound @ 7 Thfound at one thfound at th

3.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);

4.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);

5.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);

6.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);

7.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 (separated by the characters in the 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 '% ' 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.

Routines:

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;}

Results:
Splitting string "-this, a sample string." To Tokens:thisasamplestringthe end:-This
Second, MEM series1.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

2.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).

3.memcmp

int memcmp (const void * PTR1, const void * ptr2, size_t num);

similar strncmp

4.MEMCHR

void * MEMCHR (const void *buf, int ch, size_t count);

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.

Similar to STRCHR


Conversion of string to numeric type

Four, character check

Isalpha () Check if it is an alphabetic character
Isupper () Check if it is an uppercase character
Islower () Check if lowercase alphabetic characters
IsDigit () Check if it is a number
Isxdigit () Check if valid characters are represented as hexadecimal digits
Isspace () checks if the character is a space type
Iscntrl () Check if the control character
ISPUNCT () Check if it is a punctuation mark
Isalnum () Check for letters and numbers
Isprint () Check if it is a printable character

C String Processing function--view C language Help documentation

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.