string-related handler functions

Source: Internet
Author: User
Tags strcmp

1. strcpy:--Copy

Include library:

#include <string.h>

Function Prototypes:

Char *strcpy (char *dest, const char *SRC);

--Copy the string src points to the space pointed to by the dest, including the copy in SRC

Char *strncpy (char *dest, const char *SRC, size_t N);

--Copies the N-byte characters from SRC to the space pointed to by the dest, and does not intentionally add '% ' to the copy process

Parameters:

Dest, purpose

SRC, source

N, n bytes in the source

return value:

Pointer to the destination string dest

Example:

A simple implementation of the strcpy:

char * strncpy (char *dest, const char *SRC, size_t N)
{
size_t i;

for (i = 0; i < n && src[i]! = ' n '; i++)
Dest[i] = Src[i];
for (; i < n; i++)
Dest[i] = ' + ';

return dest;
}

For strncpy the current n characters do not have a string terminator, the solution is

strncpy (buf, str, n);

if (n > 0)

buf[n-1]= ' + ';

Note: The size of the target array must be large enough, otherwise there is a problem with the memory.

2. Strcat:--Connection

Included libraries:

#include <string.h>

Function Prototypes:

Char *strcat (char *dest, const char *SRC);

--Link src to dest behind, the result is dest+src. Connection method is

Start by overwriting the ' dest ' behind the string that the pointer is pointing to.

Char *strncat (char *dest, const char *SRC, size_t N);

--dest + A string of up to n src inside, special case is

When there are n or more n in the SRC, the SRC does not have to end with a '/'.

Parameters:

Dest, ...

Src...

Maximum number of n,src inside

return value:

Pointer to the destination string dest

Example:

A simple implementation of the STRNCAT:

char * STRNCAT (char *dest, const char *SRC, size_t N)
{
size_t Dest_len = strlen (dest);
size_t i;

for (i = 0; i < n && src[i]! = ' n '; i++)
Dest[dest_len + i] = src[i];
Dest[dest_len + i] = ' + ';

return dest;
}

3. strcmp: Compare

Included libraries:

#include <string.h>

Function Prototypes:

int strcmp (const char *S1, const char *S2);

--equivalent to *S1-*S2, all characters are compared

int strncmp (const char *S1, const char *S2, size_t N);

--equivalent to *S1-*S2, first n characters compared

Parameters:

S1, String 1

S2, String 2

N, (at most) the number to compare

return value:

The string that >0,s1 points to is larger than the string pointed to by S2

=0, two pointers to exactly the same string

The string that <0,s1 points to is smaller than the string pointed to by S2

Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.

string-related handler functions

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.