Explanation of string 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/n", 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] = '/0 ';
Printf ("% s", string );
Return 0;
}

@ Function name: strcat
Function prototype: char * strcat (char * str1, char * str2 );
Function: connect str2 to str1, And the '/0' at the end of 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/n", 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/n", buffer );
Strncat (buffer, "**************", 4 );
Printf ("% s/n", 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/N ");
Else
Printf ("Buffer 2 is less than buffer 1/N ");
PTR = strcmp (buf2, buf3 );
If (PTR> 0)
Printf ("Buffer 2 is greater than buffer 3/N ");
Else
Printf ("Buffer 2 is less than buffer 3/N ");
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/n", 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/N", strcspn ("abcbcadef", "CBA "));
Printf ("% d/N", strcspn ("xxxbcadef", "CBA "));
Printf ("% d/N", strcspn ("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/N", strspn ("out to lunch", "aeiou "));
Printf ("% d/N", 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/N", C, PTR-string );
Else
Printf ("the character was not found/N ");
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/n", 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/n", buffer );
P = strtok (buffer, delims );
While (P! = NULL ){
Printf ("word: % s/n", P );
P = strtok (null, delims );
}
Printf ("% s/n", 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 the terminator '/0 ')
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/N", 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/n", DEST );
PTR = memcpy (DEST, SRC, strlen (SRC ));
If (PTR)
Printf ("destination after memcpy: % s/n", 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 = '/0 ';
Printf ("the character was found: % s", DEST );
}
Else
Printf ("the character wasn't 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/N ");
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/n", 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/n", buffer );
Memset (buffer, '*', strlen (buffer)-1 );
Printf ("buffer after memset: % s", buffer );
Return 0;
}

 

Original article address
Http://tb.blog.csdn.net/TrackBack.aspx? Postid = 1708284

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.