String processing functions in the C function library frequently used in interviews

Source: Internet
Author: User

[Cpp]
# Include <stdio. h>
# Include <stdlib. h>
# Include <string. h>
# Include <assert. h>
 
 
Int myStrlen (const char * strDest)
{
Assert (NULL! = StrDest );
Const char * p = strDest;
Int len = 0;
While (* p )! = '\ 0 ')
{
Len ++;
P ++;
}
Return len;
}
 
Int itrStrlen (const char * strDest)
{
Assert (NULL! = StrDest );
Return ('\ 0 '! = * StrDest )? (1 + itrStrlen (strDest + 1): 0;
}
 
Char * myStrcpy (char * strDst, const char * strSrc)
{
Assert (NULL! = StrDst & NULL! = StrSrc );
Char * strRst = strDst;
While (* strSrc! = '\ 0 ')
* (StrDst ++) = * (strSrc ++ );
Return strRst;
}
 
Int myStrcmp (const char * strL, const char * strR)
{
Assert (NULL! = StrL & NULL! = StrR );
Int ret = 0;
Const char * left = strL;
Const char * right = strR;
While ((! (Ret = (* left-* right) & (* right! = '\ 0 '))
{
Left ++;
Right ++;
}
If (ret <0) ret =-1;
Else if (ret> 0) ret = 1;
Return ret;
}
//??? Online query
Void * myMemset (void * buffer, int c, size_t count)
{
Assert (NULL! = Buffer );
Char * p = (char *) buffer;
While (count --)
{
* P = 'C ';
Printf ("% d", * p );
P ++;
}
Return buffer;
}
 
Char * myStrchr (char * str, int c)
{
Assert (NULL! = Str );
For (; * str! = (Char) c; str ++ );
If (* str = '\ 0') return NULL;
Return str;
}
 
Char * myStrcat (char * strDes, const char * strSrc)
{
Assert (NULL! = StrDes & NULL! = StrSrc );
Char * addr = strDes;
While (* strDes! = '\ 0') strDes ++;
While (* strDes ++ = * strSrc ++ )! = '\ 0 ');
Return addr;
}
// * A little difficult
Void * myMemcpy (void * to, const void * from, size_t count)
{
Assert (NULL! = To & NULL! = From );
Void * ret =;
Char * pto = (char *);
Char * pfrom = (char *) from;
Assert (pto <pfrom | pto> (pfrom + count-1 ));//!!!
While (count --)
{
* Pto ++ = * pfrom ++;
}
Return ret;
}
 
Void test ()
{
// Test strlen
Char * s = "dfdafdaf ";
Int len = myStrlen (s );
Char * ss = "";
Len = myStrlen (ss );
Printf ("% d \ n", len );
 
// Test strcpy
Char * str = (char *) malloc (sizeof (char) * 10 );
Strcpy (str, s );
// Strcpy (str, "abcdefg ");
// Strcpy (ss, s); // an exception occurs because no memory is allocated to the ss.
Printf ("% s \ n", str );
 
// Test strcmp
Int ret = myStrcmp (str, s );
Printf ("% d \ n", ret );
Ret = myStrcmp (str, ss );
Printf ("% d \ n", ret );
Ret = myStrcmp (str, "zfdfd ");
Printf ("% d \ n", ret );
 
// Test memset
Int a [10];
MyMemset (a, 1, sizeof (int) * 10 );
 
// Test strchr
Char * p = myStrchr (s, 'A ');
Printf ("\ n % c \ n", * p );
 
// Test strcat
// Char * cat = (char *) malloc (sizeof (char) * 20 );
MyStrcat (str, "aab ");
Printf ("% s \ n", str );
 
 
}
 
 
Int main ()
{
Test ();
Return 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.