首页 > 文章列表 > C和C字符串函数的实现 - c中的字符串函数:strlen, strcat, strcpy, strcmp

C和C字符串函数的实现 - c中的字符串函数:strlen, strcat, strcpy, strcmp

140 2024-01-22

实现C和C字符串函数 1 c实现: strlen strcat strcpy strcmp 2

size_t strlen(const char *s)

{

char *ts;

for(ts =s; *s; s++)

;

return s-ts;

}

char *strcpy(char *dest, const char *src)

{

char *ts =dest;

for(; *dest++ = *src++; )

;

return ts;

}

int strcmp(const char *s1, const char *s2) { for(; *s1 == *s2; s1++, s2++) if(*s1 == '') return 0; return ((unsigned *)*s1 < (unsigned *)*s2 ? -1 : 1); } char *strcat(char *dest, const char *src) { char *ts =dest; for(; *dest; ++dest) ; for(; *src; src++) *dest++ =*src; *dest=''; return ts; }

C语言字符串处理函数

其实那些字符串函数并不复杂。任何一个的实现都不出五行代码:

char *strcpy( char *dst, const char *src ) {

char *destination = dst;

while( *dst++ = *src++ )

;

return destination;

}

char *strcat( char *dst, const char *src ) {

char *destination = dst;

while( *dst++ )

;

strcpy( --dst, src );

return destination;

}

int strcmp( const char *s1, const char *s2 ) {

for( ; *s1 == *s2; s1++, s2++ )

if( *s1 == '' ) return 0;

return *s1 - *s2;

}

unsigned strlen( const char *s ) {

const char *t = s;

while( *t++ )

;

return --t - s;

}

C语言中有哪些字符串处理函数

你可以看一下头文件string.h和stdio.h里面的相关函数声明,好多好多。

这里就不一一列出了……比如下面列出的只是其中一部分……

_CRTIMP char * __cdecl strcpy(char *, const char *);

_CRTIMP char * __cdecl strcat(char *, const char *);

_CRTIMP int __cdecl strcmp(const char *, const char *);

_CRTIMP size_t __cdecl strlen(const char *);

_CRTIMP char * __cdecl strchr(const char *, int);

_CRTIMP int __cdecl _strcmpi(const char *, const char *);

_CRTIMP int __cdecl _stricmp(const char *, const char *);

_CRTIMP int __cdecl strcoll(const char *, const char *);

_CRTIMP int __cdecl _stricoll(const char *, const char *);

_CRTIMP int __cdecl _strncoll(const char *, const char *, size_t);

_CRTIMP int __cdecl _strnicoll(const char *, const char *, size_t);

_CRTIMP size_t __cdecl strcspn(const char *, const char *);

_CRTIMP char * __cdecl _strdup(const char *);

_CRTIMP char * __cdecl _strerror(const char *);

_CRTIMP char * __cdecl strerror(int);

_CRTIMP char * __cdecl _strlwr(char *);

_CRTIMP char * __cdecl strncat(char *, const char *, size_t);

_CRTIMP int __cdecl strncmp(const char *, const char *, size_t);

_CRTIMP int __cdecl _strnicmp(const char *, const char *, size_t);

_CRTIMP char * __cdecl strncpy(char *, const char *, size_t);

_CRTIMP char * __cdecl _strnset(char *, int, size_t);

_CRTIMP char * __cdecl strpbrk(const char *, const char *);

_CRTIMP char * __cdecl strrchr(const char *, int);

_CRTIMP char * __cdecl _strrev(char *);

_CRTIMP size_t __cdecl strspn(const char *, const char *);

_CRTIMP char * __cdecl strstr(const char *, const char *);

_CRTIMP char * __cdecl strtok(char *, const char *);

_CRTIMP char * __cdecl _strupr(char *);

_CRTIMP size_t __cdecl strxfrm (char *, const char *, size_t);