Summary of Linux C string library functions

Source: Internet
Author: User
Tags strcmp

#include <strings.h>

ignores the size to compare whether two characters are equal. If S1>S2 returns a number greater than 0.
If S1 = S2 Returns a 0. If S1<s2 returns a number less than 0.

/************************************************************************************************************** ********************/

#strcmp (const char *S1, const char *S2)

#int strcasecmp (const char *S1, const char *S2);
#int strncasecmp (const char *S1, const char *S2, size_t N);
#
#DESCRIPTION
#The strcasecmp () function compares the strings S1 and S2, ignoring the case of the characters.
#It returns an integer less than, equal to, or greater than zero if S1 is found,
#respectively, to is less than, to match, or is greater than S2.
#The strncasecmp () function is similar, except it's only compares the first n characters of S1.


DEMO

/*************************************************************************    > File name:strcmp.c    > Author: Silent Lamb    > Mail: [email protected]     > Created time:2014 November 08 Saturday 21:38 59 sec. ************************* /#include <stdio.h> #include <stdlib.h> #include < String.h>int Main (int argc, char **argv) {int i = 0;if (argc = = 3) {i = strcasecmp (argv[1],argv[2]), if (i>0) {printf ("Ar GV[1] =%s  i =%d\n ", argv[1],i);} else if (i<0) {printf ("argv[2] =%s\n,i =%d\n", argv[2],i);} else{printf ("i =%d\n", i);}} Else{perror ("args error \ n"); exit (1);} Exit (0);}


*************************************************************************************************************** *******************/


Gets the first position that the character C appears in S, or the last position.
/************************************************************************************************************** ********************/
#include <strings.h>
#char *index (const char *s, int c);
#
#DESCRIPTION
#The index () function returns a pointer to the first occurrence of the character C in the string S.
#
#这个函数返回一个指针, it points to the position where the character C first appears in S.
#
#
#char *rindex (const char *s, int c);
#The Rindex () function returns a pointer to the last occurrence of the character C in the string S.
#
#这个函数返回一个指针, which points to the last occurrence of the character C in S.
#
#
#这两个参数的返回值:
#
#The index () and Rindex () functions return a pointer to the matched character or NULL if the character was not found.
#这个index () and Rindex () functions. Returns a pointer to a matching character, or null because the character was not found.

DEMO

/*************************************************************************    > File name:index.c    > Author: Silent Lamb    > Mail: [email protected]     > Created time:2014 November 08 Saturday 21:55 18 sec. ************************* /#include <stdio.h> #include <stdlib.h> #include < String.h>int Main (int argc, char **argv) {int i = 0;char * C;IF (argc = = 3) {c = index (argv[1], (*argv[2])); fprintf (stdout , "%s\n", c); Fflush (NULL);} Else{perror ("args error \ n"); exit (1);} Exit (0);}


/************************************************************************************************************** ********************/




String copy function.
/************************************************************************************************************** ********************/
#include <string.h>
#
#char *stpcpy (char *dest, const char *SRC);
#Feature Test Macro Requirements for glibc (see Feature_test_macros (7)):
#stpcpy ():
# Since GLIBC 2.10:
#_XOPEN_SOURCE >= 700 | | _posix_c_source >= 200809L
#Before GLIBC 2.1
#_GNU_SOURCE
#DESCRIPTION
#The stpcpy () function copies the string pointed to by SRC (including the terminating null byte (' ")") to the array PO Inted to by Dest.
#The strings may not overlap, and the destination string dest must is large enough to receive the copy. 。
#
#
/*************************************************************************    > File name:stpcpy.c    > Author: Silent Lamb    > Mail: [email protected]     > Created time:2014 November 08 Saturday 22:20 54 Sec. ************************* /#include <stdio.h> #include <string.h>int main (void) { Char a[200] = "ABCDE"; char *b = "dddd\n"; stpcpy (A, B);p rintf ("a =%s\n", a);}


Character array join function. SRC connected to dest back

/************************************************************************************************************** ********************/
#
#
# #include <string.h>
#char *strcat (char *dest, const char *SRC);
#char *strncat (char *dest, const char *SRC, size_t N);
#DESCRIPTION
#The strcat () function appends the SRC string to the dest string, overwriting the terminating null byte ('% ') at the E nd of Dest,
#and then adds a terminating null byte. The strings may not overlap, and the dest string must has enough space for the result. ‘‘)
#
# adds a character array to the target character array. He will add a null to the target character array as the end of the target. This character may be coincident, and adding the target array must have enough space.
#
# This function is similar to the STRNCAT function, but the STRNCAT has an input buffer bounds check

Same as the index function. Gets the position of the first occurrence of a character in a character array. The latter is the last place to appear.
/************************************************************************************************************** ********************/
# #include <string.h>
#
# char *STRCHR (const char *s, int c);
#
# char *STRRCHR (const char *s, int c);
#
# #define _GNU_SOURCE/* See Feature_test_macros (7) */
# #include <string.h>
# char *strchrnul (const char *s, int c);
#
#DESCRIPTION
#The STRCHR () function returns a pointer to the first occurrence of the character C in the string S.
#The STRRCHR () function returns a pointer to the last occurrence of the character C in the string S.
#
#//The function returns a pointer to the first position of the character C in S.
#
#//The function returns a pointer to the last position of the character C in S.
#
#
/*************************************************************************    > File name:index.c    > Author: Silent Lamb    > Mail: [email protected]     > Created time:2014 November 08 Saturday 21:55 18 sec. ************************* /#include <stdio.h> #include <stdlib.h> #include < String.h>int Main (int argc, char **argv) {int i = 0;char * C;IF (argc = = 3) {//c = index (argv[1], (*argv[2])); c = STRCHR (a Rgv[1], (*argv[2])); fprintf (stdout, "%s\n", c); Fflush (NULL);} Else{perror ("args error \ n"); exit (1);} Exit (0);}



Determine how many characters in s are not present in the accept.
/************************************************************************************************************** ********************/
#//returns how many characters in a row are not present in the accept. Return to this location.
# #include <string.h>
# size_t strspn (const char *s, const char *accept);
# size_t strcspn (const char *s, const char *reject);
#
# DESCRIPTION
# the STRSPN () function calculates the length of the initial segment of s which consists entirely of characters in accept .
#
# the STRCSPN () function calculates the length of the initial segment of s which consists entirely of characters not in Reject.
#
# RETURN VALUE
# the STRSPN () function returns the number of characters in the initial segment of s which consist only of characters From Accept.
#
# the STRCSPN () function returns the number of characters in the initial segment of S which is not in the string rejec T.
#
#
#
Divides the character string into multiple characters using a separator symbol.
After the function is called, the return value points to the pointer in front of the delimiter.
STRINGNGP points to the character following the delimiter.
/************************************************************************************************************** ********************/
#char *strsep (char **stringp, const char *delim);
#include <string.h>
#char *strsep (char **stringp, const char *delim);
#
# Feature Test Macro Requirements for glibc (see Feature_test_macros (7)):
# STRSEP (): _bsd_source
#
#DESCRIPTION
#If *STRINGP is null, the STRSEP () function returns NULL and does nothing else. Otherwise, this function finds the first token in the string *stringp,
#
# where tokens is delimited by symbols in the string Delim.
#
#This token is terminated by overwriting the delimiter with a null byte (' + ') and *STRINGP is updated-point past the T Oken.
#
#In case No delimiter were found, the token is taken to being the entire string *stringp, and *STRINGP is made NULL.
#
#RETURN VALUE
#
#The strsep () function returns a pointer to the token, which is, it returns the original value of *STRINGP.
#
#

/*************************************************************************    > File name:index.c    > Author: Silent Lamb    > Mail: [email protected]     > Created time:2014 November 08 Saturday 21:55 18 sec. ************************* /#include <stdio.h> #include <stdlib.h> #include < String.h>int Main (int argc, char **argv) {int i = 0;char * C;IF (argc = = 3) {//c = index (argv[1], (*argv[2]));//c = STRCHR (Argv[1], (*argv[2]));. /a.out name=zshh0604 =//name = zshh0604c = Strsep (&argv[1],argv[2]); fprintf (stdout, "%s\n", c); fprintf (stdout, "%s\ N ", argv[1]); Fflush (NULL);} Else{perror ("args error \ n"); exit (1);} Exit (0);}

Finds a substring in a function.
/************************************************************************************************************** ********************/
#include <string.h>
#
# char *strstr (const char *haystack, const char *needle);
#
# #define _GNU_SOURCE/* See Feature_test_macros (7) */
#
# #include <string.h>
#
# char *strcasestr (const char *haystack, const char *needle);
#
# DESCRIPTION
# the STRSTR () function finds the first occurrence of the substring needle in the string haystack. The terminating null bytes (' \ ") is not compared.")


/*************************************************************************    > File name:strstr.c    > Author: Silent Lamb    > Mail: [email protected]     > Created time:2014 November 08 Saturday 23:18 25 sec. ************************* /#include <stdio.h> #include <stdlib.h> #include < String.h>int Main (void) {char * a = "aaaabbbbb"; char * b = "AB"; char *c = Null;c = Strstr (A, B);p rintf ("c =%s", c); exit (0 );}



Copy S to the space of a malloc and then return it.
Char *strdup (const char *s);

Randomly swaps characters in a character array.
Char *strfry (char *string);


Finds the position of the character array s in how a character first appears in Accpet.
Char *strpbrk (const char *s, const char *accept);


Use Delim to find the character array.
Char *strtok (char *s, const char *delim);

Quite with copy functions.
size_t strxfrm (char *dest, const char *SRC, size_t N);

Summary of Linux C string library functions

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.