C语言字符串函数实现源码汇总
2008年05月18日 星期日 16:54
1 /* 2 * linux/lib/string.c 3 * 4 * Copyright (C) 1991, 1992 Linus Torvalds 5 */ 6 7 /* 8 * stupid library routines.. The optimized versions should generally be found 9 * as inline code in <asm-xx/string.h> 10 * 11 * These are buggy as well.. 12 */ 13 14 #include <linux/types.h> 15 16 char * ___strtok = NULL; 17 18 char * strcpy(char * dest,const char *src) 19 { 20 char *tmp = dest; 21 22 while ((*dest++ = *src++) != '\0') 23 ; 24 return tmp; 25 } 26 27 char * strncpy(char * dest,const char *src,size_t count) 28 { 29 char *tmp = dest; 30 31 while (count-- && (*dest++ = *src++) != '\0') 32 /* nothing */; 33 34 return tmp; 35 } 36 37 char * strcat(char * dest, const char * src) 38 { 39 char *tmp = dest; 40 41 while (*dest) 42 dest++; 43 while ((*dest++ = *src++) != '\0') 44 ; 45 46 return tmp; 47 } 48 49 char * strncat(char *dest, const char *src, size_t count) 50 { 51 char *tmp = dest; 52 53 if (count) { 54 while (*dest) 55 dest++; 56 while ((*dest++ = *src++)) { 57 if (--count == 0) 58 break; 59 } 60 } 字符串函数源码61 62 return tmp; 63 } 64 65 int strcmp(const char * cs,const char * ct) 66 { 67 register signed char __res; 68 69 while (1) { 70 if ((__res = *cs - *ct++) != 0 || !*cs++) 71 break; 72 } 73 74 return __res; 75 } 76 77 int strncmp(const char * cs,const char * ct,size_t count) 78 { 79 register signed char __res = 0; 80 81 while (count) { 82 if ((__res = *cs - *ct++) != 0 || !*cs++) 83 break; 84 count--; 85 } 86 87 return __res; 88 } 89 90 char * strchr(const char * s,char c) 91 { 92 for(; *s != c; ++s) 93 if (*s == '\0') 94 return NULL; 95 return (char *) s; 96 } 97 98 size_t strlen(const char * s) 99 { 100 const char *sc; 101 102 for (sc = s; *sc != '\0'; ++sc) 103 /* nothing */; 104 return sc - s; 105 } 106 107 size_t strnlen(const char * s, size_t count) 108 { 109 const char *sc; 110 111 for (sc = s; *sc != '\0' && count--; ++sc) 112 /* nothing */; 113 return sc - s; 114 } 115 116 size_t strspn(const char *s, const char *accept) 117 { 118 const char *p; 119 const char *a; 120 size_t count = 0; 121 122 for (p = s; *p != '\0'; ++p) { 123 for (a = accept; *a != '\0'; ++a) { 124 if (*p == *a) 125 break; 126 } 127 if (*a == '\0') 128 return count; 129 ++count; 130 } 131 132 return count; 133 } 134 135 char * strpbrk(const char * cs,const char * ct) 136 { 137 const char *sc1,*sc2; 138 139 for( sc1 = cs; *sc1 != '\0'; ++sc1) { 140 for( sc2 = ct; *sc2 != '\0'; ++sc2) { 141 if (*sc1 == *sc2) 142 return (char *) sc1; 143 } 144 } 145 return NULL; 146 } 147 148 char * strtok(char * s,const char * ct) 149 { 150 char *sbegin, *send; 151 152 sbegin = s ? s : ___strtok; 153 if (!sbegin) { 154 return NULL; 155 } 156 sbegin += strspn(sbegin,ct); 157 if (*sbegin == '\0') { 158 ___strtok = NULL; 159 return( NULL ); 160 } 161 send = strpbrk( sbegin, ct); 162 if (send && *send != '\0') 163 *send++ = '\0'; 164 ___strtok = send; 165 return (sbegin); 166 } 167 168 void * memset(void * s,char c,size_t count) 169 { 170 char *xs = (char *) s; 171 172 while (count--) 173 *xs++ = c; 174 175 return s; 176 } 177 178 char * bcopy(const char * src, char * dest, int count) 179 { 180 char *tmp = dest; 181 182 while (count--) 183 *tmp++ = *src++; 184 185 return dest; 186 } 187 188 void * memcpy(void * dest,const void *src,size_t count) 189 { 190 char *tmp = (char *) dest, *s = (char *) src; 191 192 while (count--) 193 *tmp++ = *s++; 194 195 return dest; 196 } 197 198 void * memmove(void * dest,const void *src,size_t count) 199 { 200 char *tmp, *s; 201 202 if (dest <= src) { 203 tmp = (char *) dest; 204 s = (char *) src; 205 while (count--) 206 *tmp++ = *s++; 207 } 208 else { 209 tmp = (char *) dest + count; 210 s = (char *) src + count; 211 while (count--) 212 *--tmp = *--s; 213 } 214 215 return dest; 216 } 217 218 int memcmp(const void * cs,const void * ct,size_t count) 219 { 220 const unsigned char *su1, *su2; 221 signed char res = 0; 222 223 for( su1 = cs, su2 = ct; 0 < count; ++su1, ++su2, count--) 224 if ((res = *su1 - *su2) != 0) 225 break; 226 return res; 227 } 228 229 /* 230 * find the first occurrence of byte 'c', or 1 past the area if none 231 */ 232 void * memscan(void * addr, unsigned char c, size_t size) 233 { 234 unsigned char * p = (unsigned char *) addr; 235 236 while (size) { 237 if (*p == c) 238 return (void *) p; 239 p++; 240 size--; 241 } 242 return (void *) p; 243 } |
1. strlen(),计算字符串长度
int strlen (const char *s)
{
const char *p;
for (p = s; *p; ++p);
return p - s;
}
2. strcpy(), 字符串拷贝.
char *stpcpy (char *dst, const char *src)
{
while ((*dst++ = *src++) != '\0');
return dst - 1;
}
3. strcat(), 字符串的连接.
char *strcat (char *dst, const char *src)
{
char *ret = dst;
while (*dst++ != '\0');
--dst;
while ((*dst++ = *src++) != '\0');
return ret;
}
4. streql(), 判断两个字符串是否相等.
int streql(char *str1,char *str2)
{
while((*str1==*str2)&&(*str1))
{
str1++;
str2++;
}
return((*str1==NULL)&&(*str2==NULL));
}
5. strchr(), 在字符串中查某个字符.
char *strchr (const char *s, int c)
{
for (; *s != (char) c; ++s)
if (*s == '\0')
return NULL;
return (char *) s;
}
6. chrcnt(), 计算某个字符在字符串中出现的次数.
int chrcnt(const char *string,int letter)
{
int count=0;
while(*string)
{
if(*string==letter)
count++;
string++;
}
return count;
}
7. strcmp(), 判断两个字符串是否相等.
int strcmp (const char *s1, const char *s2)
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系QQ:729038198,我们将在24小时内删除。
发表评论