Common C/C ++ library functions during the interview

Source: Internet
Author: User
<PRE>
// @ Yansha: End character '\ 0' must be added to the end of the string. Otherwise, the output will be misplaced.
Char * strncpy (char * strdes, const char * strsrc, unsigned int count)
{
Assert (strdes! = NULL & strsrc! = NULL );
Char * address = strdes;
While (count -- & * strsrc! = '\ 0 ')
* Strdes ++ = * strsrc ++;
* Strdes = '\ 0 ';
Return address;
}

// Locate the first occurrence of character C in string s
Char * strchr (const char * STR, int C)
{
Assert (STR! = NULL );
For (; * Str! = (Char) C; ++ Str)
If (* STR = '\ 0 ')
Return NULL;
Return STR;
}

Int strcmp (const char * s, const char * t)
{
Assert (s! = NULL & T! = NULL );
While (* S & * T & * s = * t)
{
++ S;
++ T;
}
Return (* s-* t );
}

Char * strcat (char * strdes, const char * strsrc)
{
Assert (strdes! = NULL) & (strsrc! = NULL ));
Char * address = strdes;
While (* strdes! = '\ 0 ')
++ Strdes;
While (* strdes ++ = * strsrc ++ )! = '\ 0 ')
NULL;
Return address;
}

Int strlen (const char * Str)
{
Assert (STR! = NULL );
Int Len = 0;
While (* STR ++! = '\ 0 ')
++ Len;
Return Len;
}

// Modify this function as follows:
Char * strdup _ (char * strsrc)
// Copy the string to a new position
{
If (strsrc! = NULL)
{
Char * Start = strsrc;
Int Len = 0;
While (* strsrc ++! = '\ 0 ')
Len ++;

Char * address = (char *) malloc (LEN + 1 );
Assert (address! = NULL );

While (* address ++ = * Start ++ )! = '\ 0 ');
Return address-(LEN + 1 );
}
Return NULL;
}

// Thanks for laoyi19861011.
Char * strstr (const char * strsrc, const char * Str)
{
Assert (strsrc! = NULL & Str! = NULL );
Const char * s = strsrc;
Const char * t = STR;
For (; * strsrc! = '\ 0'; ++ strsrc)
{
For (S = strsrc, t = STR; * t! = '\ 0' & * s = * t; ++ S, ++ T)
NULL;
If (* t = '\ 0 ')
Return (char *) strsrc;
}
Return NULL;
}

Char * strncpy (char * strdes, const char * strsrc, unsigned int count)
{
Assert (strdes! = NULL & strsrc! = NULL );
Char * address = strdes;
While (count -- & * strsrc! = '\ 0 ')
* Strdes ++ = * strsrc ++;
* Strdes = '\ 0 ';
Return address;
}

Char * strncat (char * strdes, const char * strsrc, unsigned int count)
{
Assert (strdes! = NULL) & (strsrc! = NULL ));
Char * address = strdes;
While (* strdes! = '\ 0 ')
++ Strdes;
While (count -- & * strsrc! = '\ 0 ')
* Strdes ++ = * strsrc ++;
* Strdes = '\ 0 ';
Return address;
}

Int strncmp (const char * s, const char * t, unsigned int count)
{
Assert (S! = NULL) & (T! = NULL ));
While (* S & * T & * s = * T & count --)
{
++ S;
++ T;
}
Return (* s-* t );
}

Char * strpbrk (const char * strsrc, const char * Str)
{
Assert (strsrc! = NULL) & (STR! = NULL ));
Const char * s;
While (* strsrc! = '\ 0 ')
{
S = STR;
While (* s! = '\ 0 ')
{
If (* strsrc = * s)
Return (char *) strsrc;
++ S;
}
++ Strsrc;
}
Return NULL;
}

Int strcspn (const char * strsrc, const char * Str)
{
Assert (strsrc! = NULL) & (STR! = NULL ));
Const char * s;
Const char * t = strsrc;
While (* t! = '\ 0 ')
{
S = STR;
While (* s! = '\ 0 ')
{
If (* t = * s)
Return T-strsrc;
++ S;
}
++ T;
}
Return 0;
}

Int strspn (const char * strsrc, const char * Str)
{
Assert (strsrc! = NULL) & (STR! = NULL ));
Const char * s;
Const char * t = strsrc;
While (* t! = '\ 0 ')
{
S = STR;
While (* s! = '\ 0 ')
{
If (* t = * s)
Break;
++ S;
}
If (* s = '\ 0 ')
Return T-strsrc;
++ T;
}
Return 0;
}

Char * strrchr (const char * STR, int C)
{
Assert (STR! = NULL );
Const char * s = STR;
While (* s! = '\ 0 ')
++ S;
For (-- S; * s! = (Char) C; -- S)
If (S = Str)
Return NULL;
Return (char *) S;
}

Char * strrev (char * Str)
{
Assert (STR! = NULL );
Char * s = STR, * t = STR, C;
While (* t! = '\ 0 ')
++ T;
For (-- t; S <t; ++ S, -- t)
{
C = * s;
* S = * t;
* T = C;
}
Return STR;
}

Char * strnset (char * STR, int C, unsigned int count)
{
Assert (STR! = NULL );
Char * s = STR;
For (; * s! = '\ 0' & S-str <count; ++ S)
* S = (char) C;
Return STR;
}

Char * strset (char * STR, int C)
{
Assert (STR! = NULL );
Char * s = STR;
For (; * s! = '\ 0'; ++ S)
* S = (char) C;
Return STR;
}

Char * strtok (char * strtoken, const char * Str)
{
Assert (strtoken! = NULL & Str! = NULL );
Char * s = strtoken;
Const char * t = STR;
While (* s! = '\ 0 ')
{
T = STR;
While (* t! = '\ 0 ')
{
If (* s = * t)
{
* (Strtoken + (S-strtoken) = '\ 0 ';
Return strtoken;
}
++ T;
}
++ S;
}
Return NULL;
}

Char * strupr (char * Str)
{
Assert (STR! = NULL );
Char * s = STR;
While (* s! = '\ 0 ')
{
If (* s> = 'A' & * S <= 'Z ')
* S-= 0x20;
S ++;
}
Return STR;
}

Char * strlwr (char * Str)
{
Assert (STR! = NULL );
Char * s = STR;
While (* s! = '\ 0 ')
{
If (* s> = 'A' & * S <= 'Z ')
* S + = 0x20;
S ++;
}
Return STR;
}

Void * memcpy (void * DEST, const void * SRC, unsigned int count)
{
Assert (DEST! = NULL) & (SRC! = NULL ));
Void * address = DEST;
While (count --)
{
* (Char *) DEST = * (char *) SRC;
DeST = (char *) DEST + 1;
Src = (char *) SRC + 1;
}
Return address;
}

Void * memccpy (void * DEST, const void * SRC, int C, unsigned int count)
{
Assert (DEST! = NULL) & (SRC! = NULL ));
While (count --)
{
* (Char *) DEST = * (char *) SRC;
If (* (char *) src = (char) C)
Return (char *) DEST + 1 );
DeST = (char *) DEST + 1;
Src = (char *) SRC + 1;
}
Return NULL;
}

Void * memchr (const void * Buf, int C, unsigned int count)
{
Assert (BUF! = NULL );
While (count --)
{
If (* (char *) BUF = C)
Return (void *) BUF;
Buf = (char *) BUF + 1;
}
Return NULL;
}

Int memcmp (const void * s, const void * t, unsigned int count)
{
Assert (S! = NULL) & (T! = NULL ));
While (* (char *) S & * (char *) T & * (char *) S = * (char *) T & count --)
{
S = (char *) S + 1;
T = (char *) t + 1;
}
Return (* (char *) S-* (char *) t );
}

// @ Big:
// You need to deal with the overlap between SRC and DeST. It's okay not to start moving from the tail.
// One case is that the Dest is smaller than the SRC, And the Dest must be moved from the beginning,
// The other is that there is an overlap between DEST and SRC. In this case, you must start moving from the end.
Void * memmove (void * DEST, const void * SRC, unsigned int count)
{
Assert (DEST! = NULL & SRC! = NULL );
Void * ret = DST;
Char * pdest = (char *) DEST;
Char * psrc = (char *) SRC;

// When pdest is behind psrc and the distance between the two is smaller than count, it starts to move from the tail. In other cases, it starts to move from the head.
If (pdst> psrc & (psrc + n)> pdst)
{
Pdst + = (n-1 );
Psrc + = (n-1 );

While (n> 0 ){
* Pdst -- = * psrc --;
N --;
}
}
Else {
While (n> 0 ){
* Pdst ++ = * psrc ++;
N --;
}
}

Return ret;
}

Void * memset (void * STR, int C, unsigned int count)
{
Assert (STR! = NULL );
Void * s = STR;
While (count --)
{
* (Char *) S = (char) C;
S = (char *) S + 1;
}
Return STR;
}

</PRE>

<PRE>
Void * memcpy (void * DEST, void * Source, size_t count)
{
Void * ret = DEST;
Int I = 0;
While (I <count)
{
* (Char *) DEST + I) = * (char *) Source + I );
I ++;
}
Return ret;
}

Memmove-copy source buffer to destination Buffer
;
; Purpose:
; Memmove () copies a source memory buffer to a destination memory buffer.
; This routine recognize overlapping buffers to avoid propogation.
; For cases where propogation is not a problem, memcpy () can be used.
;
; Algorithm:

Void * memmove (void * DEST, void * Source, size_t count)

{

Void * ret = DEST;

If (DEST <= source | DEST> = (source + count ))

{

// Non-overlapping Buffers
// Copy from lower addresses to higher addresses

While (count --)

* DEST ++ = * Source ++;

}

Else

{

// Overlapping Buffers
// Copy from higher addresses to lower addresses

DeST + = count-1;

Source + = count-1;

While (count --)

* Dest -- = * source --; L

}

Return ret;

}
</PRE>

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.