String Basic Operations Gallery

Source: Internet
Author: User
Tags strcmp strtok
string Basic Operations Gallery

All the string operations supported by the C language standard library are in String.h, and this file is under/usr/include/. Specifically, this file also contains access to the array. The included functions are divided into the following categories: replication, connection, comparison, lookup, and so on.

Table of Contents 1 replication 2 Connections 3 Comparison 4 find 5 other functions 6 strtok function 1 copy

  void  *  memcpy  ( void  *  Destination ,  const   void  *  source ,  size_t   num ); 
void * memmove ( void * destination , Const void * source , size_t num );
Char * strcpy ( Char * destination , Const Char * source );
Char * strncpy ( Char * destination , Const Char * source , size_t num );
memcpy: Copies num bytes of data from source to destination. Memmove: The same as memcpy. However, when the source and destination appear in the memory area overlap, it can also be handled correctly. strcpy: Copies the string from source to the destination, and the copy process persists until the null character in source. strncpy: Copy num characters from source to destination. If the source has null characters in the copy process, destination uses 0 to complement the NUM character. 2 Connections
Char strcat Char Destination Const Char source );
Char Strncat Char Destination Char Source size_t num );
Strcat: After the source is copied and connected to the destination, the null character at the end of the destination string is overwritten by the first character of source. A new null character will be added at the end of the source string. Returns a pointer to a destiination string. Strncat: When the first num character of source is copied to the destination, the operation on NULL is the same as strcat. 3 Comparison
int memcmp(Const void*PTR1,Const void*PTR2,size_t Num);
int strcmp(Const Char*str1,Const Char*str2);
int strcoll(Const Char*str1,Const Char*str2);
int strncmp(Const Char*str1,Const Char*str2,size_t Num);
size_t strxfrm(Char*Destination,Const Char*Source,size_t Num);
MEMCMP: Compares the first num bytes of PTR1 and PTR2. Returns 0 if they are equal, otherwise returns the comparison result of the first unequal byte in Ptr1 and PTR2. strcmp: Compares strings ptr1 and PTR2. Returns 0 if both lengths and characters are equal, otherwise returns the comparison result of the first unequal character in Ptr1 and PTR2. Strcoll: STR1 and str2 are compared according to the collation order of the text specified by Lccollate, lccollate and strcoll are identical when strcmp is "POSIX" or "C" strncmp: Compares the first num characters of PTR1 and PTR2. Returns 0 if they are equal, otherwise returns the comparison result of the first unequal character in Ptr1 and PTR2. STRXFRM: string conversion. Converts num characters from source to destination, making strxfrm () converted, the strcmp result is the same as the result of directly using Strcoll. 4 Find
Const void*MEMCHR(Const void*ptr,int value,size_t Num);
Const Char*STRCHR(Const Char*Str,int character);
size_t strcspn(Const Char*str1,Const Char*str2);
Const Char*strpbrk(Const Char*str1,Const Char*str2);
Const Char*STRRCHR(Const Char*Str,int character);
size_t strspn(Const Char*str1,Const Char*str2);
Const Char*strstr(Const Char*str1,Const Char*str2);
Char*strtok(Char*Str,Const Char*delimiters);
MEMCHR: Finds the first byte of value in the NUM storage space that the PTR points to, and returns a pointer to it. STRCHR: Lookup str points to the position where the first character in the string is character, returning a pointer to that position. STRCSPN: Scans the STR1 string to find the first occurrence of the character in any str2, returning the number of characters before that position in str1. STRPBRK: Scans the STR1 string to find the first occurrence of the character in any str2, returning a pointer to the position, such as not found, returning null. STRRCHR: Lookup str points to the position where the last character in the string is character, returning a pointer to that position. STRSPN: Find the length of the characters in the str1 from the first occurrence of str2, and return the length of the character in the str2. Strstr: Finds the first occurrence of the str1 in the STR2, returns a pointer to that point, and returns null if not included. Strtok: A function that is very strange to use. Examples below. This function is used to split the string and requires a circular call to complete the partition. When the loop is invoked, the passed-in string is null. Curious about how this function is implemented:
/* Strtok Example * *


int Main ()
{
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, ",.-");
}
return 0;
}
5 Other functions
void memset void ptr int value size_t num );
Char strerror int errnum );
size_t strlen Const Char str );
Memset: Starting from PTR, set the serial num bytes of memory to value values, return PTR strerror: According to Errum, returns a static string, which can be some error hints, is a string constant. Strlen: Returns the length of the string str. Length is positioned by a null character. implementation of 6 strtok function

The function strtok is interesting, and you can see from the example above that the function itself records some variable information for use the next time it is called. Where the record is. What's the record? Here's the simplest way to implement strtok in glibc. It's just a global variable: interesting.

Static Char*Olds;

/* Parse S into tokens separated by characters in Delim.
If S is NULL, the last string Strtok () was called with IS
Used. For example:
Char s[] = "-abc-=-def";
x = Strtok (S, "-"); x = "ABC"
x = Strtok (NULL, "="); x = "Def"
x = Strtok (NULL, "="); x = NULL
s = "Abc\0=-def\0"
*/
Char*
strtok(s, Delim)
Char*s;
Const Char*Delim;
{
Char*token;

if(s = = NULL)
s = olds;

/* Scan leading delimiters. */
S + + strspn (s, Delim);
if(*s = = ' ")
{
Olds = s;
returnNULL;
}

/* Find the "end of the" token. */
token = s;
s = strpbrk (token, delim);
if(s = = NULL)
/* This token finishes the string. */
Olds = __RAWMEMCHR (token, ' n ');
Else
{
/* Terminate the token and make OLDS point past it. */
*s = ' n ';
Olds = s + 1;
}
returnToken
}

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.