这篇文章主要讲解了“怎么仿写C语言string.h头文件检验字符串函数”,文中的讲解内容简单清晰,易于学习与理解,下面请大家跟着小编的思路慢慢深入,一起来研究和学习“怎么仿写C语言string.h头文件检验字符串函数”吧!
所需头文件:stdio.h
,stdlib.h
, string.h
int strlen_my(const char* src_str) { if (NULL == src_str) { return -1; } const char* p = src_str; while (*p != '\0') { p++; } return p - &(src_str[0]); } //递归 int strlen_my2(const char* src_str) { if (src_str == NULL) return -1; if (*src_str) { return strlen_my2(src_str + 1) + 1; } else { return 0; } } int main() { const char* p = "hello"; printf("%d \n",strlen_my(p)); return 0; }
int strcmp_my(const char* str_a, const char* str_b) { while ((*str_a != '\0' || *str_b != '\0') && *str_a == *str_b ) { str_a++; str_b++; } return *str_a - *str_b > 0 ? 1 : (*str_a - * str_b < 0 ? -1 : 0); } int strcmp_my2(const char* str_a, const char* str_b) { int sub = 0; while ((sub = *str_a - *str_b) == 0 && *str_a++ && *str_b++); //先赋值,指针再++ return sub > 0 ? 1 : (sub < 0 ? -1 : 0); } int strncmp_my(const char* str_a, const char* str_b, size_t front_len) { while (--front_len && *str_a != '\0' && *str_b != '\0' && *str_a == *str_b) { str_a++; str_b++; } return *str_a - *str_b > 0 ? 1 : (*str_a - *str_b < 0 ? -1 : 0); } int main() { const char* p = "hella"; const char* q = "hell"; printf("%d \n", strncmp_my(p, q, 4)); return 0; }
//strchr 返回字符串中第一个出现的字符ch char* strchr_my(const char* src_str, char ch) { if (src_str == NULL) { return NULL; } while (*src_str != '\0' && *src_str != ch) { src_str++; } return *src_str == '\0' ? NULL : src_str; } int main() { const char* p = "hello"; p = strchr_my(p, 'e'); if (p == NULL) return 0; printf("%c\n", *p); }
//strrchr查找字符串中最后一个出现的字母ch char* strrchr_my(const char* src_str, char ch) { if (NULL == src_str) return NULL; const char* p = src_str + strlen(src_str) - 1; while ((p - src_str) >= 0 && *p != ch) { p--; } if (p - src_str == -1) { return NULL; } return p; } int main() { const char* p = "hello"; //p = strchr_my(p, 'q'); p = strrchr_my(p, 'l'); if (p == NULL) return 0; printf("%c\n", *p); }
char* strstr_my(const char* src_str, const char* des_str) { if (NULL == src_str || NULL == des_str) { return NULL; } int len_src = strlen(src_str); int len_des = strlen(des_str); int i = 0; //src_str int j = 0; //des_str while (i < len_src && j < len_des) { if (src_str[i] == des_str[j]) { i++; j++; } else { i = i - j + 1; j = 0; } } if (j == len_des) { return src_str + i - j; } return NULL; } int main() { const char* p = "hello"; const char* q = "llo"; const char* rt = strstr_my(p, q); if (rt != NULL) { printf("%s\n", rt); return 0; } return 0; }
char* strcpy_my(char* des_str, const char* src_str) { if (des_str == NULL || src_str == NULL) return NULL; char* p = des_str; while (*src_str != '\0') { *p++ = *src_str++; } *p = '\0'; return des_str; } char* strncpy_my(char* des_str, const char* src_str, size_t len) { if (des_str == NULL || src_str == NULL || len <= 0) return NULL; char* p = des_str; //源字符串实际长度 int cur_len = strlen(src_str) <= len ? len : strlen(src_str); while (cur_len--) { *p++ = *src_str++; } *p = '\0'; return des_str; } int main() { const char* p = "hello"; char q[10] = {0}; if (strncpy_my(q, p, 1) != NULL) { printf("%s\n", q); } return 0; }
char* strcat_my(char* des_str, const char* src_str) { if (des_str == NULL || src_str == NULL) return NULL; int len_des = strlen(des_str); char* p = des_str + len_des; while (*src_str != '\0') { *p++ = *src_str++; } return des_str; } char* strncat_my(char* des_str, const char* src_str, size_t len) { if (des_str == NULL || src_str == NULL || len < 0) return NULL; int len_des = strlen(des_str); char* p = des_str + len_des; while (len--) { *p++ = *src_str++; } return des_str; } int main() { char p[20] = "hello"; const char* q = "world"; printf("p = %s\n", p); printf("q = %s\n", q); if (strncat_my(p, q, 3) != NULL) { printf("p = %s\n", p); } printf("q = %s\n", q); return 0; }
char* strdup_my(const char* src_str) { if (NULL == src_str) return NULL; char* newspace = (char*)malloc((strlen(src_str) + 1 ) * sizeof(char)); if (newspace == NULL) return NULL; int i = 0; while (*src_str != '\0') { newspace[i++] = *src_str++; } newspace[i] = '\0'; return newspace; } int main() { const char* p = "hello"; char* q = strdup_my(p); if (q != NULL) { printf("%s\n", q); free(q); q == NULL; } return 0; }
感谢各位的阅读,以上就是“怎么仿写C语言string.h头文件检验字符串函数”的内容了,经过本文的学习后,相信大家对怎么仿写C语言string.h头文件检验字符串函数这一问题有了更深刻的体会,具体使用情况还需要大家实践验证。这里是亿速云,小编将为大家推送更多相关知识点的文章,欢迎关注!
免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。