程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
 程式師世界 >> 編程語言 >> C語言 >> 關於C語言 >> 常見的C字符串處理函數的源代碼

常見的C字符串處理函數的源代碼

編輯:關於C語言
 

以下是一些常見的C字符串處理函數的源代碼,當然也是從網上找來的,學習它一個好處是加深理解C,另一個好外是應聘面試中的筆試常常會用到它們,呵呵。
當然,還有一些沒有列出來,或者有更好的實現方法,你可以貢獻一下哦,share your code! :)

//stirng.c=======

char *strcpy(char *strDes, const char *strSrc)
{
assert((strDes != NULL) && (strSrc != NULL));
char *address = strDes;
while ((*strDes ++ = *strSrc ++) != '\0')
NULL;
return address;
}

char *strchr_(char *str, int c)
{
assert(str != NULL);
while ((*str != (char) c) && (*str != '\0'))
str ++;
if (*str != '\0')
return str;
return NULL;
}

char *strchr(const char *str, int c)
{
assert(str != NULL);
for (; *str != (char) c; ++ str)
if (*str == '\0')
return NULL;
return (char *) str;
}

int strcmp(const char *s, const char *t)
{
assert(s != NULL && t != NULL);
while (*s && *t && *s == *t)
{
++ s;
++ t;
}
return (*s - *t);
}

char *strcat(char *strDes, const char *strSrc)
{
assert((strDes != NULL) && (strSrc != NULL));
char *address = strDes;
while (*strDes != '\0')
++ strDes;
while ((*strDes ++ = *strSrc ++) != '\0')
NULL;
return address;
}

int strlen(const char *str)
{
assert(str != NULL);
int len = 0;
while (*str ++ != '\0')
++ len;
return len;
}

char *strdup(const char *strSrc)
{
assert(strSrc != NULL);
int len = 0;
while (*strSrc ++ != '\0')
++ len;
char *strDes = (char *) malloc (len + 1);
while ((*strDes ++ = *strSrc ++) != '\0')
NULL;
return strDes;
}

char *strstr(const char *strSrc, const char *str)
{
assert(strSrc != NULL && str != NULL);
const char *s = strSrc;
const char *t = str;
for (; *t != '\0'; ++ strSrc)
{
for (s = strSrc, t = str; *t != '\0' && *s == *t; ++s, ++t)
NULL;
if (*t == '\0')
return (char *) strSrc;
}
return NULL;
}

char *strncpy(char *strDes, const char *strSrc, int count)
{
assert(strDes != NULL && strSrc != NULL);
char *address = strDes;
while (count -- && *strSrc != '\0')
*strDes ++ = *strSrc ++;
return address;
}

char *strncat(char *strDes, const char *strSrc, int count)
{
assert((strDes != NULL) && (strSrc != NULL));
char *address = strDes;
while (*strDes != '\0')
++ strDes;
while (count -- && *strSrc != '\0' )
*strDes ++ = *strSrc ++;
*strDes = '\0';
return address;
}

int strncmp(const char *s, const char *t, int count)
{
assert((s != NULL) && (t != NULL));
while (*s && *t && *s == *t && count --)
{
++ s;
++ t;
}
return (*s - *t);
}

char *strpbrk(const char *strSrc, const char *str)
{
assert((strSrc != NULL) && (str != NULL));
const char *s;
while (*strSrc != '\0')
{
s = str;
while (*s != '\0')
{
if (*strSrc == *s)
return (char *) strSrc;
++ s;
}
++ strSrc;
}
return NULL;
}

int strcspn(const char *strSrc, const char *str)
{
assert((strSrc != NULL) && (str != NULL));
const char *s;
const char *t = strSrc;
while (*t != '\0')
{
s = str;
while (*s != '\0')
{
if (*t == *s)
return t - strSrc;
++ s;
}
++ t;
}
return 0;
}

int strspn(const char *strSrc, const char *str)
{
assert((strSrc != NULL) && (str != NULL));
const char *s;
const char *t = strSrc;
while (*t != '\0')
{
s = str;
while (*s != '\0')
{
if (*t == *s)
break;
++ s;
}
if (*s == '\0')
return t - strSrc;
++ t;
}
return 0;
}

char *strrchr(const char *str, int c)
{
assert(str != NULL);
const char *s = str;
while (*s != '\0')
++ s;
for (-- s; *s != (char) c; -- s)
if (s == str)
return NULL;
return (char *) s;
}

char* strrev(char *str)
{
assert(str != NULL);
char *s = str, *t = str, c;
while (*t != '\0')
++ t;
for (-- t; s < t; ++ s, -- t)
{
c = *s;
*s = *t;
*t = c;
}
return str;
}

char *strnset(char *str, int c, int count)
{
assert(str != NULL);
char *s = str;
for (; *s != '\0' && s - str < count; ++ s)
*s = (char) c;
return str;
}

char *strset(char *str, int c)
{
assert(str != NULL);
char *s = str;
for (; *s != '\0'; ++ s)
*s = (char) c;
return str;
}

char *strtok(char *strToken, const char *str)
{
assert(strToken != NULL && str != NULL);
char *s = strToken;
const char *t = str;
while (*s != '\0')
{
t = str;
while (*t != '\0')
{
if (*s == *t)
{
*(strToken + (s - strToken)) = '\0';
return strToken;
}
++ t;
}
++ s;
}
return NULL;
}

char *strupr(char *str)
{
assert(str != NULL);
char *s = str;
while (*s != '\0')
{
if (*s >= 'a' && *s <= 'z')
*s -= 0x20;
s ++;
}
return str;
}

char *strlwr(char *str)
{
assert(str != NULL);
char *s = str;
while (*s != '\0')
{
if (*s >= 'A' && *s <= 'Z')
*s += 0x20;
s ++;
}
return str;
}

void *memcpy(void *dest, const void *src, int count)
{
assert((dest != NULL) && (src != NULL));
void *address = dest;
while (count --)
{
*(char *) dest = *(char *) src;
dest = (char *) dest + 1;
src = (char *) src + 1;
}
return address;
}

void *memccpy(void *dest, const void *src, int c, unsigned int count)
{
assert((dest != NULL) && (src != NULL));
while (count --)
{
*(char *) dest = *(char *) src;
if (* (char *) src == (char) c)
return ((char *)dest + 1);
dest = (char *) dest + 1;
src = (char *) src + 1;
}
return NULL;
}

void *memchr(const void *buf, int c, int count)
{
assert(buf != NULL);
while (count --)
{
if (*(char *) buf == c)
return (void *) buf;
buf = (char *) buf + 1;
}
return NULL;
}

int memcmp(const void *s, const void *t, int count)
{
assert((s != NULL) && (t != NULL));
while (*(char *) s && *(char *) t && *(char *) s == *(char *) t && count --)
{
s = (char *) s + 1;
t = (char *) t + 1;
}
return (*(char *) s - *(char *) t);
}

void *memmove(void *dest, const void *src, int count)
{
assert(dest != NULL && src != NULL);
void *address = dest;
while (count --)
{
*(char *) dest = *(char *) src;
dest = (char *) dest + 1;
src = (const char *)src + 1;
}
return address;
}

void *memset(void *str, int c, int count)
{
assert(str != NULL);
void *s = str;
while (count --)
{
*(char *) s = (char) c;
s = (char *) s + 1;
}
return str;
}
 

 
  1. 上一頁:
  2. 下一頁:
Copyright © 程式師世界 All Rights Reserved