[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;
}