string function Analysis

Source: Internet
Author: User
Tags strcmp

Tag:other   null   mit   mem    contains      string copy    nts   bytes   cte   

The

String function parsing
String function is included in the String.c file and is often used by C files.
1.strcpy
Function prototype: char* strcpy (char* str1,char* str2);
Function: Copy the string that str2 points to the STR1
function return: Return STR1, pointer to str1
/**
* strcpy-copy a%nul terminated string
* @dest: Wh ere to copy the string to
* @src: Where to copy the string from
*/
char * strcpy (char *dest, const char *SRC) {
Char *tmp = dest;

while ((*dest++ = *src++)! = ' + ');

return tmp;
}
2.strncpy
/**
* strncpy-copy a length-limited,%nul-terminated string
* @dest: Where to Copy the string t o
* @src: Where to copy the string from
* @count: The maximum number of bytes to copy
*
* Note that unlike Userspace strncpy, this does not%nul-pad the buffer.
* However, the result is not%nul-terminated if the source exceeds
* @count bytes.
*/
char * strncpy (char * Dest,const Char *src,size_t count)
{
Char *tmp = dest;

while (count--&& (*dest++ = *src++)! = ' + ')
/* nothing * *;

return TMP;
}
3.strcat
Function prototypes: char* strcat (char * str1,char * str2);
function function: str2 The string to str1, str1 the last ' \ ' is canceled
function return: str1
/**
* Strcat-append one%nul-terminated string to another
* @dest: The string to is appended to
* @src: The string to append to it
*/
char * strcat (char * dest, const char * src)
{
char *tmp = dest;

while (*dest)
dest++;
while ((*dest++ = *src++)! = ' + ')
;

return TMP;
}
4.strncat
/**
* Strncat-append a length-limited,%nul-terminated string to another
* @dest: The string to is appended to
* @src: The string to append to it
* @count: The maximum numbers of bytes to copy
*
* Note that in contrast to strncpy, strncat ensures the result is
* Terminated.
*/
char * STRNCAT (char *dest, const char *SRC, size_t count)
{
char *tmp = dest;

if (count) {
while (*dest)
dest++;
while ((*dest++ = *src++)) {
if (--count = = 0) {
*dest = ' + ';
Break
}
}
}

return TMP;
}
5.strcmp
The match returns 0, and the mismatch returns non 0.
Function prototype: int strcmp (char * str1,char * str2);
function function: Compare two string str1,str2.
function returns: STR1<STR2, returns a negative number; STR1=STR2, returns 0; STR1>STR2, returns a positive number.
/**
* Strcmp-compare-strings
* @cs: one string
* @ct: Another string
*/
int strcmp (const char * cs,const char * ct)
{
Register signed Char __res;

while (1) {
if ((__res = *cs-*ct++)! = 0 | |!*cs++)
Break
}

return __res;
}
6.strncmp
The match returns 0, and the mismatch returns non 0.
/**
* Strncmp-compare length-limited strings
* @cs: one string
* @ct: Another string
* @count: The maximum number of bytes to compare
*/
int strncmp (const char * cs,const char * ct,size_t count)
{
Register signed char __res = 0;

while (count) {
if ((__res = *cs-*ct++)! = 0 | |!*cs++)
Break
count--;
}

return __res;
}
7.strchr
Function prototype: char* STRCHR (char* str,char ch);
Function: Find the position of the first occurrence of the character ch in the string pointed to by Str
function return: Returns a pointer to the position, if not found, returns a null pointer
Parameter description: str-the string to be searched, ch-the character to find
/**
* Strchr-find The first occurrence of a character in a string
* @s:the string to be searched
* @c:the character to search for
*/
char * STRCHR (const char * s, int c)
{
for (; *s! = (char) c; ++s)
if (*s = = ' + ')
return NULL;
Return (char *) s;
}
8.strrchr
/**
* Strrchr-find The last occurrence of a character in a string
* @s:the string to be searched
* @c:the character to search for
*/
char * STRRCHR (const char * s, int c)
{
const char *p = s + strlen (s);
do {
if (*p = = (char) c)
Return (char *) p;
} while (--p >= s);
return NULL;
}
9.strlen
Function prototype: unsigned int strlen (char * str);
function function: Counts the number of characters in the string str (not including Terminator ' \ S ')
function return: Returns the length of the string.
/**
* Strlen-find the length of a string
* @s:the string to be sized
*/
size_t strlen (const char * s)
{
const char *SC;

for (sc = s; *sc! = '); ++sc)
/* nothing * *;
return sc-s;
}
10.strnlen
/**
* Strnlen-find the length of a length-limited string
* @s:the string to be sized
* @count: The maximum number of bytes to search
*/
size_t strnlen (const char * s, size_t count)
{
const char *SC;

for (sc = s; count--&& *sc! = '); ++sc)
/* nothing * *;
return sc-s;
}

11.memset
Function prototypes: void *memset (void *s, int c, size_t N)
function function: N bytes content in a string is set to C
function returns:
Parameter description: S to set the string, C-setting content, N-length
Owning file: <string.h>,<mem.h>
/**
* Memset-fill a region of memory with the given value
* @s:pointer to the start of the area.
* @c:the byte to fill the area with
* @count: The size of the area.
*
memset () to access IO space, use Memset_io () instead.
*/
void * memset (void * S,int c,size_t count)
{
Char *xs = (char *) s;

while (count--)
*xs++ = C;

return s;
}
12.bcopy
/**
* Bcopy-copy One area's memory to another
* @src: Where to copy from
* @dest: Where to copy to
* @count: The size of the area.
*
* Note that this is the same as memcpy (), with the arguments reversed.
* MEMCPY () is the standard and bcopy () is a legacy BSD function.
*
* You should don't use this function to access IO space, use Memcpy_toio ()
* or Memcpy_fromio () instead.
*/
char * bcopy (const char * src, char * dest, int count)
{
char *tmp = dest;

while (count--)
*tmp++ = *src++;

return dest;
}

13.memcpy
Function prototypes: void *memcpy (void *dest, const void *SRC, size_t N)
function function: string copy
function return: pointer to dest
Parameter description: src-source string, n-Copy maximum length
Owning file: <string.h>,<mem.h>
/**
* Memcpy-copy One area's memory to another
* @dest: Where to copy to
* @src: Where to copy from
* @count: The size of the area.
*
* You should don't use this function to access IO space, use Memcpy_toio ()
* or Memcpy_fromio () instead.
*/
void * memcpy (void * dest,const void *src,size_t count)
{
Char *tmp = (char *) dest, *s = (char *) src;

while (count--)
*tmp++ = *s++;

return dest;
}
14.memcmp
/**
* Memcmp-compare-Areas of memory
* @cs: One area of memory
* @ct: Another area of memory
* @count: The size of the area.
*/
int memcmp (const void * cs,const void * ct,size_t count)
{
Const unsigned char *su1, *SU2;
int res = 0;

for (SU1 = cs, su2 = ct; 0 < count; ++su1, ++SU2, count--)
if (res = *SU1-*su2)! = 0)
Break
return res;
}
15.memscan
/**
* Memscan-find a character in an area of memory.
* @addr: the memory area
* @c:the byte to search for
* @size: The size of the area.
*
* Returns the address of the first occurrence of @c, or 1 byte past
* The area if @c are not found
*/
void * Memscan (void * addr, int c, size_t size)
{
unsigned char * p = (unsigned char *) addr;

while (size) {
if (*p = = c)
return (void *) p;
p++;
size--;
}
return (void *) p;
}
16.strstr
/**
* Strstr-find the first substring in a%nul terminated string
* @s1: the string to be searched
* @s2: The string to search for
*/
char * STRSTR (const char * s1,const char * s2)
{
int L1, L2;

L2 = strlen (s2);
if (!L2)
Return (char *) s1;
L1 = strlen (S1);
while (L1 >= L2) {
l1--;
if (!memcmp (S1,S2,L2))
Return (char *) s1;
s1++;
}
return NULL;
}

17.memchr
/**
* Memchr-find a character in an area of memory.
* @s:the Memory Area
* @c:the byte to search for
* @n:the size of the area.
*
* Returns the address of the first occurrence of @c, or%null
* If @c is not found
*/
void *memchr (const void *s, int c, size_t N)
{
Const unsigned char *p = s;
while (n--! = 0) {
if ((unsigned char) c = = *p++) {
return (void *) (p-1);
}
}
return NULL;
}

string function Analysis

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.