strcat 的實作  

2009年10月8日 星期四 , Posted by 曾easy in

★ glibc strcat


#include <string.h>
#include <memcopy.h>

#undef strcat

/* Append SRC on the end of DEST. */
char *
strcat (dest, src)
char *dest;
const char *src;
{
char *s1 = dest;
const char *s2 = src;
reg_char c;

/* Find the end of the string. */
do
c = *s1++;
while (c != '\0');

/* Make S1 point before the next character, so we can increment
it while memory is read (wins on pipelined cpus). */
s1 -= 2;

do
{
c = *s2++;
*++s1 = c;
}
while (c != '\0');

return dest;
}



★ vc6.0中strcat

char * __cdecl strcat (
char * dst,
const char * src
)
{
char * cp = dst;

while( *cp )
cp++; /* find end of dst */

while( *cp++ = *src++ ) ; /* Copy src to end of dst */

return( dst ); /* return dst */

}



由這些實作來看,要注意的就是 dest 的空間要足夠(至少strlen(dest)+strlen(src)+1),且dest所指的字串一定要有'\0'結尾!

This entry was posted on 2009年10月8日 星期四 at 上午11:45 and is filed under . You can follow any responses to this entry through the comments feed .

0 意見

張貼留言