String processing in C Language string processing functions in C Language

Source: Internet
Author: User
Tags check character strtok

@ Function name: strdup
Function prototype: char * strdup (const char * s)
Function: copy a string. The target space is allocated by the function.
Function return: pointer to the copied string
Parameter description: src-source string to be copied
File: <string. h>
# Include <stdio. h>
# Include <string. h>
# Include <alloc. h>
Int main ()
{
Char * dup_str, * string = "abcde ";
Dup_str = strdup (string );
Printf ("% s", dup_str );
Free (dup_str );
Return 0;
}

@ Function name: strcpy
Function prototype: char * strcpy (char * str1, char * str2 );
Function: copy the str2 string to str1.
Function return: returns str1, which is the pointer to str1.
Parameter description:
File: <string. h>
# Include <stdio. h>
# Include <string. h>
Int main ()
{
Char string [10];
Char * str1 = "abcdefghi ";
Strcpy (string, str1 );
Printf ("the string is: % s", string );
Return 0;
}

@ Function name: strncpy
Function prototype: char * strncpy (char * dest, const char * src, int count)
Function: copy count characters in the src string to the dest string.
Function return: pointer to dest
Parameter description: dest-destination string, src-source string, and count-Number of copied characters
File: <string. h>
# Include <stdio. h>
# Include <string. h>
Int main ()
{
Char string [10];
Char * str1 = "abcdefghi ";
Strncpy (string, str1, 3 );
String [3] =;
Printf ("% s", string );
Return 0;
}

@ Function name: strcat
Function prototype: char * strcat (char * str1, char * str2 );
Function: String str2 is connected to str1, and str1 is canceled.
Function return: str1
Parameter description:
File: <string. h>
# Include <stdio. h>
# Include <string. h>
Int main ()
{
Char buffer [80];
Strcpy (buffer, "Hello ");
Strcat (buffer, "world ");
Printf ("% s", buffer );
Return 0;
}

@ Function name: strncat
Function prototype: char * strncat (char * dest, const char * src, size_t maxlen)
Function: connects the first maxlen character in the src string to the dest.
Function return:
Parameter description:
File: <string. h>
# Include <stdio. h>
# Include <string. h>
Char buffer [80];
Int main ()
{
Strcpy (buffer, "Hello ");
Strncat (buffer, "world", 8 );
Printf ("% s", buffer );
Strncat (buffer, "**************", 4 );
Printf ("% s", buffer );
Return 0;
}

@ Function name: strcmp
Function prototype: int strcmp (char * str1, char * str2 );
Function: Compares two strings str1 and str2.
Function return: str1 <str2, returns a negative number; str1 = str2, returns 0; str1> str2, returns a positive number.
Parameter description:
File: <string. h>
# Include <string. h>
# Include <stdio. h>
Int main ()
{
Char * buf1 = "aaa", * buf2 = "bbb", * buf3 = "ccc ";
Int ptr;
Ptr = strcmp (buf2, buf1 );
If (ptr> 0)
Printf ("buffer 2 is greater than buffer 1 ");
Else
Printf ("buffer 2 is less than buffer 1 ");
Ptr = strcmp (buf2, buf3 );
If (ptr> 0)
Printf ("buffer 2 is greater than buffer 3 ");
Else
Printf ("buffer 2 is less than buffer 3 ");
Return 0;
}

@ Function name: strncmp
Function prototype: int strncmp (char * str1, char * str2, int count)
Function: Compares the first count characters in str1 and str2 in alphabetical order.
Function return value: less than 0: str1 <str2, equal to 0: str1 = str2, greater than 0: str1> str2
Parameter description: str1, str2-string to be compared, count-comparison Length
File: <string. h>
# Include <string. h>
# Include <stdio. h>
Int main ()
{
Int ptr;
Char * buf1 = "aaabbb", * buf2 = "bbbccc", * buf3 = "ccc ";
Ptr = strncmp (buf2, buf1, 3 );
If (ptr> 0)
Printf ("buffer 2 is greater than buffer 1 ");
Else
Printf ("buffer 2 is less than buffer 1 ");
Ptr = strncmp (buf2, buf3, 3 );
If (ptr> 0)
Printf ("buffer 2 is greater than buffer 3 ");
Else
Printf ("buffer 2 is less than buffer 3 ");
Return (0 );
}

@ Function name: strpbrk
Function prototype: char * strpbrk (const char * s1, const char * s2)
Function: Obtain the position pointer of the first "also appearing in s2" character in s1.
Function return: Position pointer
Parameter description:
File: <string. h>
# Include <stdio. h>
# Include <string. h>
Int main ()
{
Char * p = "Find all vowels ";
While (p)
{
Printf ("% s", p );
P = strpbrk (p + 1, "aeiouAEIOU ");
}
Return 0;
}

@ Function name: strcspns
Function prototype: int strcspn (const char * s1, const char * s2)
Function: counts the length of s1 from the beginning until the first "character from s2"
Function return: Length
Parameter description:
File: <string. h>
# Include <stdio. h>
# Include <string. h>
Int main ()
{
Printf ("% d", strcspn ("abcbcadef", "CBA "));
Printf ("% d", strcspn ("xxxbcadef", "CBA "));
Printf ("% d", strcspns ("123456789", "CBA "));
Return 0;
}

@ Function name: strspns
Function prototype: int strspn (const char * s1, const char * s2)
Function: counts the length of s1 from the beginning until the first "no character from s2"
Function return: Position pointer
Parameter description:
File: <string. h>
# Include <stdio. h>
# Include <string. h>
# Include <alloc. h>
Int main ()
{
Printf ("% d", strspn ("out to lunch", "aeiou "));
Printf ("% d", strspns ("out to lunch", "xyz "));
Return 0;
}

@ Function name: strchr
Function prototype: char * strchr (char * str, char ch );
Function: locate the location of the first occurrence of the ch in the string pointed to by str.
Function return: returns a pointer to this position. If no pointer is found, a null pointer is returned.
Parameter description: str-string to be searched, ch-string to be searched
File: <string. h>
# Include <string. h>
# Include <stdio. h>
Int main ()
{
Char string [15];
Char * ptr, c = r;
Strcpy (string, "This is a string ");
Ptr = strchr (string, c );
If (ptr)
Printf ("The character % c is at position: % d", c, ptr-string );
Else
Printf ("The character was not found ");
Return 0;
}

@ Function name: strrchr
Function prototype: char * strrchr (const char * s, int c)
Function: Obtain the position pointer of the last c character in string s.
Function return: Position pointer
Parameter description:
File: <string. h>
# Include <string. h>
# Include <stdio. h>
Int main ()
{
Char string [15];
Char * ptr, c = r;
Strcpy (string, "This is a string ");
Ptr = strrchr (string, c );
If (ptr)
Printf ("The character % c is at position: % d", c, ptr-string );
Else
Printf ("The character was not found ");
Return 0;
}

@ Function name: strstr
Function prototype: char * strstr (char * str1, char * str2 );
Function: locate the position where the str2 string first appears in the str1 string (excluding the str2 string Terminator)
Function return: returns the pointer to this position. If no pointer is found, a null pointer is returned.
Parameter description:
File: <string. h>
# Include <stdio. h>
# Include <string. h>
Int main ()
{
Char * str1 = "Open Watcom C/C ++", * str2 = "Watcom", * ptr;
Ptr = strstr (str1, str2 );
Printf ("The substring is: % s", ptr );
Return 0;
}

@ Function name: strrev
Function prototype: char * strrev (char * s)
Function: sorts all characters in a string in reverse order.
Function return: pointer to s
Parameter description:
File: <string. h>
# Include <string. h>
# Include <stdio. h>
Int main ()
{
Char * forward = "string ";
Printf ("Before strrev (): % s", forward );
Strrev (forward );
Printf ("After strrev (): % s", forward );
Return 0;
}

@ Function name: strnset
Function prototype: char * strnset (char * s, int ch, size_t n)
Function: set the first n characters in string s to the ch value.
Function return: pointer to s
Parameter description:
File: <string. h>
# Include <stdio. h>
# Include <string. h>
Int main ()
{
Char * string = "abcdefghijklmnopqrstuvwxyz ";
Char letter = x;
Printf ("string before strnset: % s", string );
Strnset (string, letter, 13 );
Printf ("string after strnset: % s", string );
Return 0;
}

@ Function name: strset
Function prototype: char * strset (char * s, int ch)
Function: Set all characters in string s to ch values.
Function return: pointer to s
Parameter description:
File: <string. h>
# Include <stdio. h>
# Include <string. h>
Int main ()
{
Char string [10] = "123456789 ";
Char symbol = c;
Printf ("Before strset (): % s", string );
Strset (string, symbol );
Printf ("After strset (): % s", string );
Return 0;
}

@ Function name: strtok
Function prototype: char * strtok (char * s1, const char * s2)
Function: break down an s1 string into multiple strings separated by a specific separator (generally used to break down an English sentence into a word)
Function return: the sub-string pointer before the first occurrence of characters in s2 in string s1
Parameter description: s2 is generally set to a delimiter in s1.
Specifies that the first parameter (I .e., the second, third, and subsequent substrings of s1) must be NULL for subcalls.
After each successful match, replace the position of the substring split in s1 with NULL (the first ring in the chain is removed). Therefore, s1 is damaged.
The function remembers the pointer position for the next call.

File: <string. h>
# Include <string. h>
# Include <stdio. h>
Int main ()
{
Char * p;
Char * buffer;
Char * delims = {".,"};
Buffer = strdup ("Find words, all of them .");
Printf ("% s", buffer );
P = strtok (buffer, delims );
While (p! = NULL ){
Printf ("word: % s", p );
P = strtok (NULL, delims );
}
Printf ("% s", buffer );
Return 0;
}

@ Function name: strupr
Function prototype: char * strupr (char * s)
Function: converts characters in string s to uppercase letters.
Function return:
Parameter description:
File: <string. h>
# Include <stdio. h>
# Include <string. h>
Int main ()
{
Char * string = "abcdefghijklmnopqrstuvwxyz", * ptr;
Ptr = strupr (string );
Printf ("% s", ptr );
Return 0;
}

@ Function name: strlwr
Function prototype: char * strlwr (char * s)
Function: converts a character in a string to lowercase.
Function return: pointer to s
Parameter description:
File: <string. h>
# Include <string. h>
Int main ()
{
Char str [] = "how to say? ";
Printf ("% s", strlwr (str ));
Return 0;
}

@ Function name: strlen
Function prototype: unsigned int strlen (char * str );
Function: counts the number of characters in the str string (excluding Terminator)
Function return: returns the length of the string.
Parameter description:
File: <string. h>
# Include <stdio. h>
# Include <string. h>
Int main ()
{
Char str [] = "how are you! ";
Printf ("the lence is: % d", strlen (str ));
Return 0;
}

@ Function name: strerror
Function prototype: char * strerror (int errnum)
Function: Get the error message.
Function return: error message string pointer
Parameter description: errnum-error number
File: <string. h>
# Include <stdio. h>
# Include <errno. h>
Int main ()
{
Char * buffer;
Buffer = strerror (errno );
Printf ("Error: % s", buffer );
Return 0;
}

@ Function name: memcpy
Function prototype: void * memcpy (void * dest, const void * src, size_t n)
Function: String Copy
Function return: pointer to dest
Parameter description: src-source string, maximum n-copy Length
File: <string. h>, <mem. h>
# Include <stdio. h>
# Include <string. h>
Int main ()
{
Char src [] = "******************************";
Char dest [] = "abcdefghijlkmnopqrstuvwxyz0123456709 ";
Char * ptr;
Printf ("destination before memcpy: % s", dest );
Ptr = memcpy (dest, src, strlen (src ));
If (ptr)
Printf ("destination after memcpy: % s", dest );
Else
Printf ("memcpy failed ");
Return 0;
}

@ Function name: memccpy
Function prototype: void * memccpy (void * dest, const void * src, int c, size_t n)
Function: copy a string to a specified length or stop copying a string.
Function return:
Parameter description: src-source string pointer, c-Stop copy check character, n-length, dest-copy bottom target string pointer
File: <string. h>, <mem. h>
# Include <string. h>
# Include <stdio. h>
Int main ()
{
Char * src = "This is the source string ";
Char dest [50];
Char * ptr;
Ptr = memccpy (dest, src, c, strlen (src ));
If (ptr)
{
* Ptr =;
Printf ("The character was found: % s", dest );
}
Else
Printf ("The character wasnt found ");
Return 0;
}

@ Function name: memchr
Function prototype: void * memchr (const void * s, int c, size_t n)
Function: Search for the position of a character c in the first n characters of a string.
Function return: returns the position pointer of c. If NULL is returned, it indicates that no position is found.
Parameter description: s-the string to be searched, c-the character to be searched, and n-the specified length.
File: <string. h>, <mem. h>
# Include <string. h>
# Include <stdio. h>
Int main ()
{
Char str [17];
Char * ptr;
Strcpy (str, "This is a string ");
Ptr = memchr (str, r, strlen (str ));
If (ptr)
Printf ("The character r is at position: % d", ptr-str );
Else
Printf ("The character was not found ");
Return 0;
}

@ Function name: memcmp
Function prototype: int memcmp (const void * s1, const void * s2, size_t n)
Function: Compares the first n Bytes of the two strings s1 and s2 in alphabetical order.
Function return value: <0, = 0,> 0 respectively indicates s1 <, =,> s2
Parameter description: s1, s2-string to be compared, and n-length to be compared
File: <string. h>, <mem. h>
# Include <stdio. h>
# Include <string. h>
Int main ()
{
Char * buf1 = "ABCDE123 ";
Char * buf2 = "abcde456 ";
Int stat;
Stat = memcmp (buf1, buf2, 5 );
Printf ("The strings to position 5 are ");
If (stat) printf ("not ");
Printf ("the same ");
Return 0;
}
 
@ Function name: memicmp
Function prototype: int memicmp (const void * s1, const void * s2, size_t n)
Function: Compares the first n characters of string s1 in alphabetical order, regardless of uppercase or lowercase letters.
Function return value: <0, = 0,> 0 respectively indicates s1 <, =,> s2
Parameter description: s1, s2-string to be compared, and n-length to be compared
File: <string. h>, <mem. h>
# Include <stdio. h>
# Include <string. h>
Int main ()
{
Char * buf1 = "ABCDE123 ";
Char * buf2 = "abcde456 ";
Int stat;
Stat = memicmp (buf1, buf2, 5 );
Printf ("The strings to position 5 are ");
If (stat) printf ("not ");
Printf ("the same ");
Return 0;
}

@ Function name: memmove
Function prototype: void * memmove (void * dest, const void * src, size_t n)
Function: String Copy
Function return: pointer to dest
Parameter description: src-source string, maximum n-copy Length
File: <string. h>, <mem. h>
# Include <string. h>
# Include <stdio. h>
Int main ()
{
Char dest [40] = "abcdefghijklmnopqrstuvwxyz0123456789 ";
Printf ("destination prior to memmove: % s", dest );
Memmove (dest + 1, dest, 35 );
Printf ("destination after memmove: % s", dest );
Return 0;
}

@ Function name: memset
Function prototype: void * memset (void * s, int c, size_t n)
Function: Set the n bytes in the string to c.
Function return:
Parameter description: s-string to be set, c-set content, n-length
File: <string. h>, <mem. h>
# Include <string. h>
# Include <stdio. h>
# Include <mem. h>
Int main ()
{
Char buffer [] = "Hello world ";
Printf ("Buffer before memset: % s", buffer );
Memset (buffer, *, strlen (buffer)-1 );
Printf ("Buffer after memset: % s", buffer );
Return 0;
}

Related Article

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.