锘??xml version="1.0" encoding="utf-8" standalone="yes"?>
*char *memchr(buf, chr, cnt) - search memory for given character.
*
*Purpose:
* Searches at buf for the given character, stopping when chr is
* first found or cnt bytes have been searched through.
*
*Entry:
* void *buf - memory buffer to be searched
* int chr - character to search for
* size_t cnt - max number of bytes to search
*
*Exit:
* returns pointer to first occurence of chr in buf
* returns NULL if chr not found in the first cnt bytes
*
*Exceptions:
*
*******************************************************************************/
void * __cdecl memchr (
const void * buf,
int chr,
size_t cnt
)
{
while ( cnt && (*(unsigned char *)buf != (unsigned char)chr) ) {
buf = (unsigned char *)buf + 1;
cnt--;
}
return(cnt ? (void *)buf : NULL);
}
]]>
*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.
*
*Entry:
* void *dst = pointer to destination buffer
* const void *src = pointer to source buffer
* size_t count = number of bytes to copy
*
*Exit:
* Returns a pointer to the destination buffer
*
*Exceptions:
*******************************************************************************/
void * __cdecl mymemcpy (
void * dst,
const void * src,
size_t count
)
{
void * ret = dst;
/*
* copy from lower addresses to higher addresses
*/
while (count--) {
*(char *)dst = *(char *)src;
dst = (char *)dst + 1;
src = (char *)src + 1;
}
return(ret);
}
void * __cdecl mymemmove (
void * dst,
const void * src,
size_t count
)
{
void * ret = dst;
if (dst <= src || (char *)dst >= ((char *)src + count)) {
/*
* Non-Overlapping Buffers
* copy from lower addresses to higher addresses
*/
while (count--) {
*(char *)dst = *(char *)src;
dst = (char *)dst + 1;
src = (char *)src + 1;
}
}
else {
/*
* Overlapping Buffers
* copy from higher addresses to lower addresses
*/
dst = (char *)dst + count - 1;
src = (char *)src + count - 1;
while (count--) {
*(char *)dst = *(char *)src;
dst = (char *)dst - 1;
src = (char *)src - 1;
}
}
return(ret);
}
int _tmain(int argc, _TCHAR* argv[])
{
int i = 0;
int a[10];
for(i; i < 10; i++)
{
a[i] = i;
}
mymemcpy(&a[4], a, sizeof(int)*6);
for(i = 0; i < 10; i++)
{
printf("%d ",a[i]);
}
printf("\n");
for(i=0; i < 10; i++)
{
a[i] = i;
}
mymemmove(&a[4], a, sizeof(int)*6);
for(i = 0; i < 10; i++)
{
printf("%d ",a[i]);
}
printf("\n");
return 0;
}
Result:
0 1 2 3 0 1 2 3 0 1
0 1 2 3 0 1 2 3 4 5
]]>
*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.
*
*Entry:
* void *dst = pointer to destination buffer
* const void *src = pointer to source buffer
* size_t count = number of bytes to copy
*
*Exit:
* Returns a pointer to the destination buffer
*
*Exceptions:
*******************************************************************************/
void * __cdecl memmove (
void * dst,
const void * src,
size_t count
)
{
void * ret = dst;
if (dst <= src || (char *)dst >= ((char *)src + count)) {
/*
* Non-Overlapping Buffers
* copy from lower addresses to higher addresses
*/
while (count--) {
*(char *)dst = *(char *)src;
dst = (char *)dst + 1;
src = (char *)src + 1;
}
}
else {
/*
* Overlapping Buffers
* copy from higher addresses to lower addresses
*/
dst = (char *)dst + count - 1;
src = (char *)src + count - 1;
while (count--) {
*(char *)dst = *(char *)src;
dst = (char *)dst - 1;
src = (char *)src - 1;
}
}
return(ret);
}
]]>
*char *memset(dst, val, count) - sets "count" bytes at "dst" to "val"
*
*Purpose:
* Sets the first "count" bytes of the memory starting
* at "dst" to the character value "val".
*
*Entry:
* void *dst - pointer to memory to fill with val
* int val - value to put in dst bytes
* size_t count - number of bytes of dst to fill
*
*Exit:
* returns dst, with filled bytes
*
*Exceptions:
*
*******************************************************************************/
void * __cdecl memset (
void *dst,
int val,
size_t count
)
{
void *start = dst;
while (count--) {
*(char *)dst = (char)val;
dst = (char *)dst + 1;
return(start);
}
]]>
*memcpy - Copy source buffer to destination buffer
*
*Purpose:
* memcpy() copies a source memory buffer to a destination memory buffer.
* This routine does NOT recognize overlapping buffers, and thus can lead
* to propogation.
*
* For cases where propogation must be avoided, memmove() must be used.
*
*Entry:
* void *dst = pointer to destination buffer
* const void *src = pointer to source buffer
* size_t count = number of bytes to copy
*
*Exit:
* Returns a pointer to the destination buffer
*
*Exceptions:
*******************************************************************************/
void * __cdecl memcpy (
void * dst,
const void * src,
size_t count
)
{
void * ret = dst;
/*
* copy from lower addresses to higher addresses
*/
while (count--) {
*(char *)dst = *(char *)src;
dst = (char *)dst + 1;
src = (char *)src + 1;
}
return(ret);
}
]]>
{
char *cp; /* traverses string for C locale conversion */
for (cp = string; *cp; ++cp)
{
if ('a' <= *cp && *cp <= 'z')
*cp += 'A' - 'a';
}
return(string);
} /* C locale */
]]>
*char *strchr(string, c) - search a string for a character
*
*Purpose:
* Searches a string for a given character, which may be the
* null character '\0'.
*
*Entry:
* char *string - string to search in
* char c - character to search for
*
*Exit:
* returns pointer to the first occurence of c in string
* returns NULL if c does not occur in string
*
*Exceptions:
*
*******************************************************************************/
char * __cdecl strchr (
const char * string,
int ch
)
{
while (*string && *string != (char)ch)
string++;
if (*string == (char)ch)
return((char *)string);
return(NULL);
}
]]>
*char *_strset(string, val) - sets all of string to val
*
*Purpose:
* Sets all of characters in string (except the terminating '/0'
* character) equal to val.
*
*
*Entry:
* char *string - string to modify
* char val - value to fill string with
*
*Exit:
* returns string -- now filled with val's
*
*Uses:
*
*Exceptions:
*
*******************************************************************************/
char * __cdecl _strset (
char * string,
int val
)
{
char *start = string;
while (*string)
*string++ = (char)val;
return(start);
}
]]>
*char *_strdup(string) - duplicate string into malloc'd memory
*
*Purpose:
* Allocates enough storage via malloc() for a copy of the
* string, copies the string into the new memory, and returns
* a pointer to it.
*
*Entry:
* char *string - string to copy into new memory
*
*Exit:
* returns a pointer to the newly allocated storage with the
* string in it.
*
* returns NULL if enough memory could not be allocated, or
* string was NULL.
*
*Uses:
*
*Exceptions:
*
*******************************************************************************/
char * __cdecl _strdup (
const char * string
)
{
char *memory;
if (!string)
return(NULL);
if (memory = malloc(strlen(string) + 1))
return(strcpy(memory,string));
return(NULL);
}
]]>
*char *strcat(dst, src) - concatenate (append) one string to another
*
*Purpose:
* Concatenates src onto the end of dest. Assumes enough
* space in dest.
*
*Entry:
* char *dst - string to which "src" is to be appended
* const char *src - string to be appended to the end of "dst"
*
*Exit:
* The address of "dst"
*
*Exceptions:
*
*******************************************************************************/
char * __cdecl strcat (
char * dst,
const char * src
)
{
char * cp = dst; // 寰楀埌棣栧湴鍧, 鐒跺悗閬嶅巻鍒版渶鍚? 鍐峜opy
while( *cp )
cp++; /* find end of dst */
while( *cp++ = *src++ ) ; /* Copy src to end of dst */
return( dst ); /* return dst */
}
{
char s1[] = "abc";
char s2[] = "def";
char* p = my_strcat(s1, s2);
return 0;
}
]]>
*char *strstr(string1, string2) - search for string2 in string1
*
*Purpose:
* finds the first occurrence of string2 in string1
*
*Entry:
* char *string1 - string to search in
* char *string2 - string to search for
*
*Exit:
* returns a pointer to the first occurrence of string2 in
* string1, or NULL if string2 does not occur in string1
*
*Uses:
*
*Exceptions:
*
*******************************************************************************/
char * __cdecl strstr (
const char * str1,
const char * str2
)
{
char *cp = (char *) str1;
char *s1, *s2;
if ( !*str2 )
return((char *)str1);
while (*cp)
{
s1 = cp;
s2 = (char *) str2;
while ( *s1 && *s2 && !(*s1-*s2) )
s1++, s2++;
if (!*s2)
return(cp);
cp++;
}
return(NULL);
}
{
char* p1 = "abcde";
char* p2 = "cd";
char* p = my_strstr(p1, p2);
return 0;
}
]]>
*strcmp - compare two strings, returning less than, equal to, or greater than
*
*Purpose:
* STRCMP compares two strings and returns an integer
* to indicate whether the first is less than the second, the two are
* equal, or whether the first is greater than the second.
*
* Comparison is done byte by byte on an UNSIGNED basis, which is to
* say that Null (0) is less than any other character (1-255).
*
*Entry:
* const char * src - string for left-hand side of comparison
* const char * dst - string for right-hand side of comparison
*
*Exit:
* returns -1 if src < dst
* returns 0 if src == dst
* returns +1 if src > dst
*
*Exceptions:
*
*******************************************************************************/
int __cdecl strcmp (
const char * src,
const char * dst
)
{
int ret = 0 ;
while( ! (ret = *(unsigned char *)src - *(unsigned char *)dst) && *dst)
++src, ++dst;
if ( ret < 0 )
ret = -1 ;
else if ( ret > 0 )
ret = 1 ;
return( ret );
}
{
char* p1 = "abcde";
char* p2 = "abchd";
int i = my_strcmp(p1, p2);
return 0;
}
]]>
*strlen - return the length of a null-terminated string
*
*Purpose:
* Finds the length in bytes of the given string, not including
* the final null character.
*
*Entry:
* const char * str - string whose length is to be computed
*
*Exit:
* length of the string "str", exclusive of the final null byte
*
*Exceptions:
*
*******************************************************************************/
size_t __cdecl strlen ( const char * str )
{
const char *eos = str;
while( *eos++ ) ;
return( (int)(eos - str - 1) );
}
{
char str[] = "abcde";
int n = strlen(str);
return 0;
}
]]>
*char *strcpy(dst, src) - copy one string over another
*
*Purpose:
* Copies the string src into the spot specified by
* dest; assumes enough room.
*
*Entry:
* char * dst - string over which "src" is to be copied
* const char * src - string to be copied over "dst"
*
*Exit:
* The address of "dst"
*
*Exceptions:
*******************************************************************************/
char * __cdecl strcpy(char * dst, const char * src)
{
char * cp = dst;
while( *cp++ = *src++ )
; /* Copy src over dst */
return( dst );
}
{
char* src = "abc";
char* dest = (char*)malloc(strlen(src)+1);
memset(dest, 0x00, sizeof(strlen(src)+1));
dest = strcpy(dest, src);
free(dest);
dest = NULL;
return 0;
}
]]>
*strrev.c - reverse a string in place
*
* Copyright (c) 1985-1997, Microsoft Corporation. All rights reserved.
*
*Purpose:
* defines _strrev() - reverse a string in place (not including
* '\0' character)
*
*******************************************************************************/
#include <cruntime.h>
#include <string.h>
/***
*char *_strrev(string) - reverse a string in place
*
*Purpose:
* Reverses the order of characters in the string. The terminating
* null character remains in place.
*
*Entry:
* char *string - string to reverse
*
*Exit:
* returns string - now with reversed characters
*
*Exceptions:
*
*******************************************************************************/
char * __cdecl _strrev (char * string)
{
// 姝ゆ紼嬪簭鐨剆tart/left/string 閮藉湪鍚屼竴鍧楀唴瀛樹腑, start璁板綍鐨勬槸璧峰鍦板潃,鏈彉鍔ㄨ繃
// left鍜宻tring 鐢變簬瑕佷氦鎹㈡暟鎹? 鎵浠ヤ細鏈夊彉鍖?/span>
char *start = string;
char *left = string;
char ch;
while (*string++) /* find end of string */
;
string -= 2; // 鎸囬拡鍚庨2, 寰楀埌鏈鍚庝竴涓瓧絎︾殑鎸囬拡鍦板潃.
// 姝ゆ閫昏緫灝辨槸 鍓嶅悗瀛楃浜ゆ崲, 姣斿 "abcde":
// 絎竴嬈″驚鐜? a 鍜?nbsp;e 浜ゆ崲鍚?nbsp;鍙樻垚ebcda,
// 絎簩嬈″驚鐜痓鍜宒浜ゆ崲, 鍙樻垚edcba,
// 絎笁嬈$敱浜巐eft==string, 鏁呭驚鐜仠姝?
while (left < string)
{
ch = *left;
*left++ = *string; // 姝ゅ鍙垎瑙d負涓ゆ鏇村ソ鐞嗚В: *left =*sting; left++; 鎶婂間粯緇?left, 鐒跺悗left鍚戝悗縐諱綅
*string-- = ch; // 鍚屼笂: *string = ch; string--; 鎶婂間粯緇?string, 鐒跺悗string鍚戝墠縐諱綅
}
return(start);
}
鍙﹀涓縐嶅埄鐢╯trlen鍑芥暟鏉ュ緱鍒板瓧絎︿覆闀垮害鐨勬柟娉? 鍏跺師鐞嗚窡涓婇潰鐨勭浉鍚? 棣栧熬渚濇浜ゆ崲鍒頒腑闂翠負姝?
{
int n = strlen(p);
int i = 0;
int j = 0;
char ch;
for (i = 0, j = n-1; i < j; i++, j--)
{
ch = p[i];
p[i] = p[j];
p[j]=ch;
}
return p;
}
{
// 姝ゅ涓嶈兘瀹氫箟涓篶har* p = "abcde";
// 鍥犱負鍦╯trrev閲岄潰瑕佷慨鏀筽鐨勫唴瀹?
char p[] = "abcde";
char* p1 = strrev(p);
char* p2 = my_strrev(p);
return 0;
}
]]>