strlen #include <string.h> size_t strlen( char *str ); |
功能:函數返回字符串str 的長度( 即空值結束符以前字符數目)。c++
非遞歸實現
less
size_t my_strlen(const char* src) { assert(src); size_t len = 0; while (*src++ != '\0') ++len; return len; }
遞歸實現ide
int my_strlen2(const char* strDest) //遞歸求字符串長度 { assert(strDest); if ('\0' == *strDest) return 0; else return (1 + my_strlen(++strDest)); }
strcpy #include <string.h> char *strcpy( char *to, const char *from ); |
功能:複製字符串from 中的字符到字符串to,包括空值結束符。返回值爲指針to。函數
不考慮內存重疊spa
char* my_strcpy(char* dst, const char* src) { assert(dst && src); char* pdst = dst; while ((*pdst++ = *src++) != '\0'); return dst; }
考慮內存重疊指針
char *my_strcpy(char *dst,const char *src) { assert(dst && src); char *ret = dst; my_memcpy(dst,src,strlen(src)+1); return ret; }
其中my_memcpy實現以下:orm
void * my_memcpy(void *dst, const void *src, size_t count) { assert(dst && src); char* pdst = (char*)dst; char* psrc = (char*)src; int n = count; if (pdst > psrc && pdst < (psrc + count)) //重疊 { for (size_t i = n - 1; i != -1; --i) pdst[i] = psrc[i]; } else { for (size_t i = 0; i < n; ++i) pdst[i] = psrc[i]; } return dst; }
strncpy #include <string.h> char *strncpy( char *to, const char *from, size_t count ); |
功能:將字符串from 中至多count個字符複製到字符串to中。若是字符串from 的長度小於count,其他部分用'\0'填補。返回處理完成的字符串。遞歸
char* my_strncpy(char* dst, const char* src, size_t n) { assert(dst && str); char* pdst = dst; const char* psrc = src; while (n && (*pdst++ = *psrc ++) != '\0') { --n; } if (n) //若是n==0 則下面會死循環 { while (n--) { *pdst++ = '\0'; } } return dst; }
strstr #include <string.h> char *strstr( const char *str1, const char *str2 ); |
功能:函數返回一個指針,它指向字符串str2 首次出現於字符串str1中的位置,若是沒有找到,返回NULL。內存
char* my_strstr(const char* src, const char* sub) { assert(src && sub); int sublen = strlen(sub); char* srctmp = NULL; char* subtmp = NULL; while (strlen(src) >= sublen) //strlen(src)在不斷更新,能夠提早結束循環 { srctmp = (char*)src; subtmp = (char*)sub; while (*srctmp == *subtmp) { if (*subtmp == '\0') return (char*)src; ++srctmp; ++subtmp; } if (*subtmp == '\0') return (char*)src; ++src; } return NULL; }
strcat #include <string.h> char *strcat( char *str1, const char *str2 ); |
功能:函數將字符串str2 鏈接到str1的末端,並返回指針str1.字符串
char* my_strcat(char* dst, const char* str) { assert(dst && str); char* pdst = dst; while (*pdst != '\0') //注意這裏不能爲 *pdst++ != '\0' 這樣會多加一次! ++pdst; while (*str != '\0') *pdst++ = *str++; *pdst = '\0'; return dst; }
strncat #include <string.h> char *strncat( char *str1, const char *str2, size_t count ); |
功能:將字符串from 中至多count個字符鏈接到字符串to中,追加空值結束符。返回處理完成的字符串。
char* my_strncat(char *dst, const char *str, size_t n) { assert(dst && str); char* pdst = dst; while (*pdst != '\0') ++pdst; while (n && (*pdst = *str) != '\0') { --n; ++pdst; //最好這樣作,不要放在while裏面 ++str; } *pdst = '\0'; return dst; }
strcmp #include <string.h> int strcmp( const char *str1, const char *str2 ); |
功能:比較字符串str1 and str2, 返回值以下:
返回值 |
解釋 |
less than 0 |
str1 is less than str2 |
equal to 0 |
str1 is equal to str2 |
greater than 0 |
str1 is greater than str2 |
int my_strcmp(const char *str1, const char *str2) { assert(str1 && str2); const char* pstr1 = str1; const char* pstr2 = str2; while (*pstr1 && *pstr2 && (*pstr1 == *pstr2)) { ++pstr1; ++pstr2; } return *pstr1 - *pstr2; }
strncmp #include <string.h> int strncmp( const char *str1, const char *str2, size_t count ); |
功能:比較字符串str1 和 str2中至多count個字符。返回值以下:
返回值 |
解釋 |
less than 0 |
str1 is less than str2 |
equal to 0 |
str1 is equal to str2 |
greater than 0 |
str1 is greater than str2 |
若是參數中任一字符串長度小於count, 那麼當比較到第一個空值結束符時,就結束處理。
int my_strncmp(const char *str1, const char *str2, size_t n) { assert(str1 && str2 && n > 0); const char* pstr1 = str1; const char* pstr2 = str2; while (--n && *pstr1 && *pstr2 && (*pstr1 == *pstr2)) { ++pstr1; ++pstr2; } return *pstr1 - *pstr2; }