您的位置:首页 > 编程语言

微软strcpy,strcat和strcmp、memcpy的实现源代码

2010-11-05 16:07 507 查看
1. /***
2. *char *strcpy(dst, src) - copy one string over another
3. *
4. *Purpose:
5. * Copies the string src into the spot specified by
6. * dest; assumes enough room.
7. *
8. *Entry:
9. * char * dst - string over which "src" is to be copied
10. * const char * src - string to be copied over "dst"
11. *
12. *Exit:
13. * The address of "dst"
14. *
15. *Exceptions:
16. *******************************************************************************/
17. char* strcpy(char * dst, const char * src)
18. {
19. char * cp = dst;
20. while( *cp++ = *src++ )
21. ; /* Copy src over dst */
22. return( dst );
23. }
24. /***
25. *char *strcat(dst, src) - concatenate (append) one string to another
26. *
27. *Purpose:
28. * Concatenates src onto the end of dest. Assumes enough
29. * space in dest.
30. *
31. *Entry:
32. * char *dst - string to which "src" is to be appended
33. * const char *src - string to be appended to the end of "dst"
34. *
35. *Exit:
36. * The address of "dst"
37. *
38. *Exceptions:
39. *
40. *******************************************************************************/
41. Char* strcat ( char * dst , const char * src )
42. {
43. char * cp = dst;
44. while( *cp )
45. cp++; /* find end of dst */
46. while( *cp++ = *src++ ) ; /* Copy src to end of dst */
47. return( dst ); /* return dst */
48. }
49. /***
50. *strcmp - compare two strings, returning less than, equal to, or greater than
51. *
52. *Purpose:
53. * STRCMP compares two strings and returns an integer
54. * to indicate whether the first is less than the second, the two are
55. * equal, or whether the first is greater than the second.
56. *
57. * Comparison is done byte by byte on an UNSIGNED basis, which is to
58. * say that Null (0) is less than any other character (1-255).
59. *
60. *Entry:
61. * const char * src - string for left-hand side of comparison
62. * const char * dst - string for right-hand side of comparison
63. *
64. *Exit:
65. * returns -1 if src < dst
66. * returns 0 if src == dst
67. * returns +1 if src > dst
68. *
69. *Exceptions:
70. *
71. *******************************************************************************/
72. int strcmp ( const char* src, const char* dst )
73. {
74. int ret = 0 ;
75. while( ! (ret = *(unsigned char *)src - *(unsigned char *)dst) && *dst)
76. ++src, ++dst;
77. if ( ret < 0 )
78. ret = -1 ;
79. else if ( ret > 0 )
80. ret = 1 ;
81. return( ret );
82. }
83. *******************************************************************************/
84. char * strstr(const char * str1, const char * str2)
85. {
86. char * cp = (char *)str1;
87. char *s1, *s2;
88. if (!*str2)
89.    return (char *)str1;
90. while (*cp)
91. {
92.    s1 = cp;
93.    s2 = str2;
94.
95.    while (*s1 && *s2 && !(*s1-*s2))
96.     s1++, s2++;
97.
98.    while (!*s2)
99.     return cp;
100.
101.    cp++;
102. }
103. return NULL;
104. }
105.
106. *******************************************************************************/
107. void * memcpy (void * dst, const void * src, size_t count)
108. {
109.
110. void * ret = dst;
111.
112. while (count--) {
113.
114. *(char *)dst = *(char *)src;
115. dst = (char *)dst + 1;
116. src = (char *)src + 1;
117. }
118. return(ret);
119.
120. }
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: