1.strlen(字符串长度计算函数)
•字符串以 '\0' 作为结束标志,strlen函数返回的是在字符串中 '\0' 前面出现的字符个数(不包
含 '\0' )。
• 参数指向的字符串必须要以 '\0' 结束。
• 注意函数的返回值为size_t,是无符号的( 易错 )
• strlen的使用需要包含头⽂件
• 下面是strlen函数的模拟实现
1.方法一
int my_strlen(const char* str) { assert(str);//判断指针的有效性 int count = 0; while (*str) { str++; count++; } return count; }
2.方法二
int my_strlen(const char* str) { assert(str);//判断指针的有效性 //不创建临时变量的计数器 if (*str == '\0') return 0; else return 1 + my_strlen(str + 1); }
3.方法三
int my_strlen(const char* str) { assert(str);//判断指针的有效性 const char* s = str; //指针减指针的方法 while (*s) { s++; } return s - str; }
下面是库里面strlen的实现方法
size_t __cdecl strlen ( const char * str ) { const char *eos = str; while( *eos++ ) ; return( eos - str - 1 ); }
2.strcpy(字符串拷贝函数)
• 源字符串必须以 '\0' 结束。
• 会将源字符串中的 '\0' 拷贝到目标空间。
• 目标空间必须足够大,以确保能存放源字符串。
• 目标空间必须可修改。
• 下面是strcpy函数的模拟实现
char* my_strcpy(char* dest, const char* srs) { assert(dest && srs); char* ret = dest; while (*dest++ = *srs++) { ; } return ret; }
3.strcat(字符串追加函数)
• 源字符串必须以 '\0' 结束。
• 目标字符串中也得有 \0 ,否则没办法知道追加从哪里开始。
• 目标空间必须有足够的大,能容纳下源字符串的内容。
• 目标空间必须可修改。
• 下面是strcpy函数的模拟实现
char* my_strcat(char* dest, const char* src) { assert(dest && src); char* p = dest; while (*dest) { dest++; } while ((*dest++ = *src++)) { ; } return p; }
下面是库里面strcat的实现代码~
char * __cdecl strcat ( char * dst, const char * src ) { char * cp = dst; while( *cp ) cp++; /* find end of dst */ while((*cp++ = *src++) != '\0') ; /* Copy src to end of dst */ return( dst ); /* return dst */ }
4.strcmp(字符串大小比较函数)
• 标准规定:
◦ 第⼀个字符串小于第⼆个字符串,则返回大于0的数字
◦ 第⼀个字符串等于第⼆个字符串,则返回0
第⼀个字符串小于第⼆个字符串,则返回小于0的数字
◦ 那么如何判断两个字符串?比较两个字符串中对应位置上字符ASCII码值的大小。
strcmp函数的模拟实现:
int * my_strcmp(const char* str1, const char* str2) { assert(str1 && str2); while (*str1 == *str2) { if (*str1 == '\0') return 0; str1++; str2++; } return *str1 - *str2; }
下面是库里面strcmp的源代码
int __cdecl strcmp ( const char * src, const char * dst ) { int ret = 0 ; while((ret = *(unsigned char *)src - *(unsigned char *)dst) == 0 && *dst) { ++src, ++dst; } return ((-ret) < 0) - (ret < 0); // (if positive) - (if negative) generates branchless code }
5.strncpy(有限制的字符串拷贝函数)
举个栗子啦~
#include <stdio.h> #include <string.h> int main() { char str1[] = "To be or not to be"; char str2[40]; char str3[40]; /* copy to sized buffer (overflow safe): */ strncpy(str2, str1, sizeof(str2)); /* partial copy (only 5 chars): */ strncpy(str3, str2, 5); str3[5] = '\0'; /* null character manually added */ puts(str1); puts(str2); puts(str3); return 0; }
运行结果~
strncpy库里面的源代码如下啦~
char * __cdecl strncpy ( char * dest, const char * source, size_t count ) { char *start = dest; while (count && (*dest++ = *source++) != '\0') /* copy string */ count--; if (count) /* pad out with zeroes */ while (--count) *dest++ = '\0'; return(start); }
6.strncat(有限制的字符串追加函数)
举个栗子啦~
库里面strncat的源代码在下面啦~
char * __cdecl strncat ( char * front, const char * back, size_t count ) { char *start = front; while (*front++) ; front--; while (count--) if ((*front++ = *back++) == 0) return(start); *front = '\0'; return(start); }
7.strncmp(有限制的字符串大小比较函数)
举个栗子啦~
#include <stdio.h> #include <string.h> int main () { char str[][5] = { "R2D2" , "C3PO" , "R2A6" }; int n; puts ("Looking for R2 astromech droids..."); for (n=0 ; n<3 ; n++) if (strncmp (str[n],"R2xx",2) == 0) { printf ("found %s\n",str[n]); } return 0; }
运行结果~
库里面strncmp的源代码在下面啦~
int __cdecl strncmp ( const char *first, const char *last, size_t count ) { size_t x = 0; if (!count) { return 0; } /* * This explicit guard needed to deal correctly with boundary * cases: strings shorter than 4 bytes and strings longer than * UINT_MAX-4 bytes . */ if( count >= 4 ) { /* unroll by four */ for (; x < count-4; x+=4) { first+=4; last +=4; if (*(first-4) == 0 || *(first-4) != *(last-4)) { return(*(unsigned char *)(first-4) - *(unsigned char *)(last-4)); } if (*(first-3) == 0 || *(first-3) != *(last-3)) { return(*(unsigned char *)(first-3) - *(unsigned char *)(last-3)); } if (*(first-2) == 0 || *(first-2) != *(last-2)) { return(*(unsigned char *)(first-2) - *(unsigned char *)(last-2)); } if (*(first-1) == 0 || *(first-1) != *(last-1)) { return(*(unsigned char *)(first-1) - *(unsigned char *)(last-1)); } } } /* residual loop */ for (; x < count; x++) { if (*first == 0 || *first != *last) { return(*(unsigned char *)first - *(unsigned char *)last); } first+=1; last+=1; } return 0; }
8.strstr(字符串查找函数)
举个栗子啦~
#include <stdio.h> #include <string.h> int main () { char str[] ="This is a simple string"; char * pch; pch = strstr (str,"simple"); if (pch != NULL) strncpy (pch,"sample",6); puts (str); return 0; }
strstr的模拟是实现~
char* strstr(const char* str1, const char* str2) { char* cp = (char*)str1; char* s1, * s2; if (!*str2) return((char*)str1); while (*cp) { s1 = cp; s2 = (char*)str2; while (*s1 && *s2 && !(*s1 - *s2)) s1++, s2++; if (!*s2) return(cp); cp++; } return(NULL); }
9.strtok(字符串切割函数)
举个栗子啦~
#include <stdio.h> #include <string.h> int main () { char str[] ="- This, a sample string."; char * pch; printf ("Splitting string \"%s\" into tokens:\n",str); pch = strtok (str," ,.-"); while (pch != NULL) { printf ("%s\n",pch); pch = strtok (NULL, " ,.-"); } return 0; }
10.strerror(打印错误信息函数)
举个栗子啦~
#include <stdio.h> #include <string.h> #include <errno.h> int main () { FILE * pFile; pFile = fopen ("unexist.ent","r"); if (pFile == NULL) printf ("Error opening file unexist.ent: %s\n",strerror(errno)); return 0; }