strcat的实现
- #include <stdio.h>
- char* strcat(char*dest,char *src)
- {
- char *d=dest;
- char *s=src;
- while(*d)
- d++;
- while(*s)
- {
- *d=*s;
- d++;
- s++;
- }
- *d='\0';
- return dest;
- }
- int main(void)
- {
- char str[20]="hello";
- char *p="world";
- strcat(str,p);
- printf("%s\n",str);
- return 0;
- }
- //更简洁
- char* 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 *///一直到赋完'\0'
- return( dst ); /* return dst */
- }
转载于:https://blog.51cto.com/liangbing8612/701721