常见字符串函数的实现
2017-12-24 20:52
225 查看
strlen(),strcpy()
strncpy(),strcat(),strncat()
strchr(),strrchr(),strnchr()
strstr()
strcmp(),strncmp()
count_chars()
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)); }
相关文章推荐
- 常见字符串函数的模拟实现(二)(c语言)
- 字符串函数模拟实现之strlen函数的三种常见实现方法
- 常见字符串函数的实现过程
- C语言常见字符串函数实现
- 常见的字符串函数之strlen函数,strcpy函数,strcat函数的实现;
- 常见几个字符串函数实现代码
- 几个常见字符串函数的实现
- 常见字符串函数的模拟实现(一)(c语言)
- C语言常见字符串函数实现
- 几个常见字符串函数的实现
- 七种常见排序算法及实现
- 用Java实现几种常见的排序算法
- android txt小说阅读器的实现(完美实现分页阅读,支持常见编码格式)
- 常见排序算法及JAVA实现
- 常见对象_在大串中查找小串出现的次数案例代码实现
- 几种常见的数据结构的java实现
- Appium 实现iPhone真机自动化-常见问题
- 在网上看到使用PHP实现常见的数据结构和算法
- 企业实现服务器负载均衡常见的四种方法
- 常见设计模式的解析和实现(C++)之十-Proxy模式