常见字符串函数的实现
来源:互联网 发布:php微信抢红包源代码 编辑:程序博客网 时间:2024/05/21 14:47
strlen(),strcpy()
int my_strlen(const char *str)//字符串长度{ assert(str != NULL); int len = 0; while (*str ++ != '\0') { ++ len; } return len; }char *my_strcpy(char *di,const char *si)//字符串的拷贝{ assert((di != NULL) && (si != NULL)); char * cp = di; while(*si) { *di++ = *si++; } *di= '\0'; return cp; }void main(){ char str[100]=""; char *dtr="hello word"; int n=my_strlen(dtr); char *p=my_strcpy(str,dtr); printf("字符串长度:%d\n",n); printf("字符串拷贝后:%s\n",p);}
strncpy(),strcat(),strncat()
char *my_strncpy(char *di, const char *si, int n)//字符串的长度受限拷贝{ assert((di != NULL) && (si != NULL)); char *ncpy = di; while(n--) { if(*si != '\0') { *di++ = *si++; } else { *di++ = '\0'; } } return ncpy; }char *my_strcat(char *di, const char *si)//两个字符串的连接 { assert((di != NULL) && (si != NULL)); char *link = di; while (*di != '\0') { ++ di; } while ((*di ++ = *si ++) != '\0') { } return link; }char *my_strncat(char *di, const char *si, int n) // 两个字符串的受限连接{ assert((di != NULL) && (si != NULL)); char *nlink = di; while (*di != '\0') { ++ di; } while (n-- && *si != '\0' ) { *di ++ = *si ++; } *di = '\0'; return nlink; } void main(){ char str[100]=""; char ctr[100]="ff"; char ftr[100]="ff"; char *dtr="hello word"; printf("字符串长度受限拷贝后:%s\n",my_strncpy(str,dtr,5)); printf("字符串连接后:%s\n",my_strcat(ctr,dtr)); printf("字符串受限连接后:%s\n",my_strncat(ftr,dtr,7));}
strchr(),strrchr(),strnchr()
char *my_strchr( char *di, char c)// 查找字符串中首次出现字符c的位置,找到返回指针,找不到返回NULL { assert(di != NULL); for (; *di !=c; ++ di) { if (*di == '\0') { return NULL; } } return di; }char *my_strrchr( char *si, char c)//查找字符串中最后一次次出现字符c的位置,找到返回指针,找不到返回NULL { assert(si != NULL); char *p = NULL; while(*si != '\0') { if(*si == (char)c) { p = (char *)si; } si++; } return p;}char *my_strnchr( const char *di,char c,int n)//字符串查找给定字符,第 n 次出现的位置{ assert(di != NULL); int count=0; for (; *di !='\0'; ++ di) { if (*di==c) { count+=1; } if(count==n) { return (char *)di; } } return NULL;}void main(){ char *str="hello a waord"; char *ctr="hello a waard"; printf("前查找字符后:%s\n",my_strchr(str,'a')); printf("后查找字符后:%s\n",my_strrchr(str,'a')); printf("查找第n次字符出现后:%s\n",my_strnchr(ctr,'a',3));}
strstr()
char *my_strstr(const char *di ,const char *si)//查找字符串(子串) { assert(di != NULL && si != NULL); const char *s = di; const char *t = si; for (; *di != '\0'; ++ di) { for (s = di, t = si; *t != '\0' && *s == *t; ++s, ++t) { NULL; } if (*t == '\0') { return (char *) di; } } return NULL;} void main(){ char *str="hello word"; char *ctr="word"; printf("%s\n",my_strstr(str,ctr));}
strcmp(),strncmp()
int my_strcmp(const char *di, const char *si) //字符串比较,返回负,零,正数 { assert(di != NULL && si != NULL); while(*di && *si && *di == *si) { ++ di; ++ si; } return (*di - *si); }int my_strncmp(const char *di, const char *si, int n)//受限字符串的比较 { assert((di != NULL) && (si != NULL)); while (*di && *si && *di == *si && n --) { ++ di; ++ si; } return(*di - *si); }void main(){ char *str="hellobword"; char *ctr="helloaword"; printf("%d\n",my_strcmp(str,ctr)); printf("%d\n",my_strncmp(str,ctr,4));}
count_chars()
int count_chars(char const *di,char const *si)//在第一个参数中进行查找, 并返回匹配第二个参数所包含的字符的数量 { assert((di != NULL) && (si != NULL)); int count = 0; for (int i = 0;i < strlen(di);i++ ) { for(int j = 0;j < strlen(si);j++) { if( di[i] == si[j]) { count++; } } } return count; } void main(){ char *str="abcdefg"; char *ctr="acdfyuilo"; printf("相同字符数:%d\n",count_chars(str,ctr));}
阅读全文