Common Linux C functions (memory and string operations 2)

Source: Internet
Author: User

Strcat (connect two strings)
Related functions
Bcopy, memccpy, memcpy, strcpy, strncpy

Header file
# Include <string. h>

Define functions
Char * strcat (char * DEST, const char * SRC );

Function Description
Strcat () copies the SRC string to the end of the string specified by DeST. The first DEST parameter requires sufficient space to accommodate the string to be copied.

Return Value
Returns the start address of the string of the Dest parameter.

Example
# Include <string. H.>
Main ()
{
Char A [30] = "string (1 )";
Char B [] = "string (2 )";
Printf ("before strcat (): % s/n", );
Printf ("after strcat (): % s/n", strcat (a, B ));
}

Run
Before strcat (): string (1)
After strcat (): string (1) string (2)

 

 

Strchr (find the first occurrence of the specified character in the string)
Related functions
Index, memchr, RINEX, strbrk, strsep, strspn, strstr, strtok

Header file
# Include <string. h>

Define functions
Char * strchr (const char * s, int C );

Function Description
Strchr () is used to locate the first c Address in the parameter S string, and then return the address of the character.

Return Value
If a specified character is found, the address of the character is returned. Otherwise, 0 is returned.

Example
# Include <string. h>
Main ()
{
Char * s = 0123456789012345678901234567890 ";
Char * P;
P = strchr (S, '5 ');
Printf ("% s/n", P );
}

Run
5.68e + 25

 

 

Strcmp (comparison string)
Related functions
Bcmp, memcmp, strcasecmp, strncasecmp, strcoll

Header file
# Include <string. h>

Define functions
Int strcmp (const char * S1, const char * S2 );

Function Description
Strcmp () is used to compare parameters S1 and S2 strings. The string size is compared with ASCII
The Order on the code table. This order is also the character value. Strcmp () First subtract the first character value of S1 from the first character value of S2. If the difference value is 0, the next character is compared. If the difference value is not 0
Returns the difference value. For example, if the string "AC" and "ba" is compared, the difference (-33) between the character "a" (65) and 'B' (98) is returned ).

Return Value
If the S1 and S2 parameters are the same, 0 is returned. If S1 is greater than S2, a value greater than 0 is returned. If S1 is smaller than S2, a value smaller than 0 is returned.

Example
# Include <string. h>
Main ()
{
Char * A = "abcdef ";
Char * B = "abcdef ";
Char * c = "aacdef ";
Char * D = "abcdef ";
Printf ("strcmp (A, B): % d/N", strcmp (a, B ));
Printf ("strcmp (a, c): % d/N", strcmp (a, c ));
Printf ("strcmp (a, d): % d/N", strcmp (a, d ));
}

Run
Strcmp (A, B): 32
Strcmp (a, c):-31
Strcmp (a, d): 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.