【C语言】C语言字符串函数实现(二)
来源:互联网 发布:淘宝网小衫 编辑:程序博客网 时间:2024/03/29 01:07
6.字符串的查找(模式匹配)
char *my_strstr(const char *string1, const char *string2);
7.统计匹配(失配)的字符个数
匹配:
size_t my_strspn(const char *string, const char *accept);
失配:
size_t my_strcspn(const char *string, const char *reject);
8.字符串分割函数
char *my_strtok(char *string, const char *delim);
9.内存操作
移动:
void *my_memove(void *des_str, void *src_str, size_t size);
6.字符串的查找(模式匹配)
char *my_strstr(const char *string1, const char *string2){ const char *str1 = string1; const chat *str2 = string2; if(string1 == NULL || string2 == NULL) return NULL; } while(*str1 && *str1){ if(*str1 == *str2){ str1++; str2++; }else{ str1 -= (str2 - string2 -1); str2 = string2; } } if(*str2 == '\0'){ return (str1 -= (str2 - string2)); } return NULL;}
7.统计匹配(失配)的字符个数
匹配:
size_t my_strspn(const chat *string, const char *accept){ char ch_table[256] = {0}; const char *str = string; const char *acc = accept; int match_count = 0; if(string == NULL) hash_char(acc,ch_table); //把accept字符串元素进行散列 while(*str != '\0'){ if(ch_table[*str]){ match_count++; }else{ return match_count; } str++; } return match_count;}static void hash_char(const char *accept, char *ch_table){ while(*accept != '\0'){ ch_table[*accept]++; accept++; }}
失配:
size_t my_strcspn(const char *string, const char *reject){ char ch_table[256] = {0}; const char *str = string; const char rej = reject; int match_count = 0; if(string == NULL) hash_char(rej,ch_table); //把accept字符串元素进行散列 while(*str != '\0'){ if(!ch_table[*str]){ match_count++; }else{ return match_count; } str++; } return match_count;}static void hash_char(const char *reject, char *ch_table){ while(*reject != '\0'){ ch_table[*reject]++; reject++; }}
8.字符串分割函数
#define ASCII_SIZE (32)char *my_strtok(char *string, const char *delim){ unsigned char *str = NULL; char arr[ASCII_SIZE] = {0}; const unsigned char *delimit=(const unsigned char*)delimiter; static unsigned char *last = NULL; do{ arr[*delimit >> 3] |= (1 << (*delimit &7)); }while(*delimit != '\0'); if(string == NULL){ str = last; }else{ str = (unsigned char *)string; } while(arr[*str >> 3] & (1 << (*str & 7)) && *str){ str++; } for( ; *str; ++str){ if(arr[*str >> 3] & (1 << (*str & 7))){ *str++ = '\0'; break; } } last = str; if(string == (char *)str){ return NULL; }else{ return string; }}
9.内存操作
移动:
void *my_memove(void *des_str, void *src_str, size_t size);{ char *dest = dest_str; const char *src = src_str; if(dest_str == NULL || src_str == NULL || dest_str == stc_str){ return dest_str; } if(dest > src && (dest - src) < size){ while(size--){ dest[size] = src[size]; } }else{ while(size--){ *dest++ = *src++; } } return dest_str;}
0 0
- 【C语言】C语言字符串函数实现(二)
- 字符串处理函数C语言实现(二)
- C语言字符串函数集锦(二)
- C语言字符串函数集锦(二)
- 【C语言】C语言字符串函数实现(一)
- C语言实现字符串函数
- C语言常用字符串函数的实现二
- C语言常用字符串处理函数(二)
- C语言函数(二)
- c语言(二)函数
- C语言字符串函数
- C语言字符串函数
- C语言字符串函数
- C语言字符串函数
- C语言字符串函数
- C语言字符串函数
- c语言 字符串函数
- C语言字符串函数
- 安卓在代码中设置TextView的drawableLeft、drawableRight、drawableTop、drawableBottom
- 为什么 LR 模型要使用 sigmoid 函数,背后的数学原理是什么?
- 第1章 概述--主流PCB设计工具软件简介
- 页面间旋转跳转
- Cordova 环境搭建(Window)
- 【C语言】C语言字符串函数实现(二)
- Mysql常用的命令
- BlockingQueue
- 《你必须知道的.NET》读书笔记:内存分配等
- 如何向一个Fragment传递参数---setArguments方法的介绍
- 基于PaaS平台批量初始化普通用户信息方法整理
- torch+lua 编辑第一个程序 hello world
- 黏性Session和非黏性Session
- C++的Json解析库:jsoncpp和boost