字符串自实现(一)(mystrcpy,mystrcat,mystrcmp)
char* mystrcpy(char* str_one,const char* str_two)
{
char* tmp = str_one;
while (*str_one++ = *str_two++)return tmp;
}
char* mystrcat(char* str_one,const char* str_two)
{
char* tmp = str_one;
while (*str_one)str_one++;
while (*str_one++ = *str_two++)return tmp;
} //1-7为逐渐优化过程
int mystrcmp1(const char* str_one,const char* str_two)
{
while (*str_one != '\0'&&*str_two != '\0')
{
if (*str_one > *str_two)
{
return 1;
}
else if (*str_one < *str_two)
{
return -1;
}
else
{
str_one++;
str_two++;
}
}
if (*str_one != '\0'&&*str_two == '\0')
{
return 1;
}
else if (*str_one == '\0'&&*str_two != '\0')
{
return -1;
}
else
{
return 0;
}
} int mystrcmp2(const char* str_one, const char* str_two)
{
while (*str_one != '\0'&&*str_two != '\0')
{
if (*str_one != *str_two)
{
return *str_one - *str_two;
}
else
{
str_one++;
str_two++;
}
}
return *str_one - *str_two;
} int mystrcmp3(const char* str_one, const char* str_two)
{
while (*str_one != '\0'&&*str_two != '\0')
{
if (*str_one != *str_two)
{
break;
}
else
{
str_one++;
str_two++;
}
}
return *str_one - *str_two;
} int mystrcmp4(const char* str_one, const char* str_two)
{
for (;*str_one != '\0'&&*str_two != '\0';str_one++,str_two++)
{
if (*str_one != *str_two)
{
break;
}
}
return *str_one - *str_two;
} int mystrcmp5(const char* str_one, const char* str_two)
{
for (; *str_one&&*str_two; str_one++, str_two++)
if (*str_one != *str_two)
break;
return *str_one - *str_two;
} int mystrcmp6(const char* str_one, const char* str_two)
{
for (; (*str_one&&*str_two) && (*str_one == *str_two); str_one++, str_two++);
return *str_one - *str_two;
} int mystrcmp7(const char* str_one, const char* str_two)
{
for (; (*str_one&&*str_two) && (*str_one++ == *str_two++););
return *str_one - *str_two;
}
字符串自实现(一)(mystrcpy,mystrcat,mystrcmp)的更多相关文章
- [c/c++] programming之路(22)、字符串(三)——字符串封装
项目结构 头文件.h #include<stdio.h> #include<stdlib.h> #include<string.h> //字符串封装,需要库函数 / ...
- C语言程序设计(十) 字符串
第十章 字符串 字符串常量是由一对双引号括起来的一个字符串序列 字符串实际就是由若干个有效数字构成且以字符'\0'作为结束的一个字符序列 C语言没有提供字符串数据类型,因此字符串的存取要用字符型数组来 ...
- 自定义方法实现strcpy,strlen, strcat, strcmp函数,了解及实现原理
位置计算字符串长度 //strlen()函数,当遇到'\0'时,计算结束,'\0'不计入长度之内 //字符串的拷贝 //strcpy(字符串1,字符串2); //把字符串2 ...
- 自己实现C++的string类
使用C++的朋友对string类都不会陌生,string类使用起来很是方便,比C的字符数组好用多了,但是我们能不能自己实现一个属于自己的string类呢? 实现一个具有基本功能的string类并不难, ...
- 1. C/C++项目一
需求: 使用C语言封装string 字符串,实现字符串的增.删.改.查等API函数. 要求: 不能使用 string 库函数,所有库函数必须自己手动实现. [项目实现] myString.h 代码如下 ...
- typedef的使用2——定义函数
#include <stdio.h> #include <string.h> #pragma warning(disable:4996) //闲言碎语都先不要讲了,直接上函数吧 ...
- 睡不着,复习一下C++基础中的基础(深拷贝与浅拷贝)
#include <iostream> #include <string> #include <assert.h> using namespace std; //声 ...
- C语言学习第八章
今天开始学习字符串,学完以后最低要做到了解什么是字符串,以及字符串的一些应用方法,毕竟字符串还是很常见的. 简单的字符串"helloworld",这个字符串大家应该都很眼熟,学编程 ...
- c 跟字符串有关的函数
1.字符串查找 strstr char * strstr(const char *s1, const char *s2); 在s1中查找s2,如果找到返回首个s2的首地址 char * strcase ...
随机推荐
- SpringBoot整合redis实现过期key监听事件
Spring整合redis实现key过期事件监听:https://www.cnblogs.com/pxblog/p/13969375.html 可以用于简单的过期订单取消支付.7天自动收货场景中 1. ...
- RPA项目POC指南:概念、步骤与技巧
"为什么部署RPA前要进行POC?RPA不是开箱即用吗?" 其实,RPA的实施并非总是一帆风顺,"碰坑"在所难免. 据安永报告显示,30%至50%的初始RPA项 ...
- c++ vector释放概述
这里仅为概述,具体的详情分析,可以Google下,会得到更详尽的解释. 1.函数clear和erase都将数组的size清空,但对应的空间并没有回收,而且,一直push_baclk, 对应的capac ...
- 网络编程之UDP(2)linux查看socket默认缓冲区大小和最大值
默认值 命令: cat /proc/sys/net/core/rmem_default 结果: 最大值 命令: cat /proc/sys/net/core/rmem_max 结果:
- 【LeetCode】42. Trapping Rain Water 接雨水 (C++)
作者: 负雪明烛 id: fuxuemingzhu 个人博客:http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 暴力求解 保存左右最大值 单调栈 日期 题目地址:ht ...
- 【LeetCode】245. Shortest Word Distance III 解题报告 (C++)
作者: 负雪明烛 id: fuxuemingzhu 个人博客:http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 字典+暴力检索 日期 题目地址:https://lee ...
- 【LeetCode】362. Design Hit Counter 解题报告 (C++)
作者: 负雪明烛 id: fuxuemingzhu 个人博客:http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 字典 日期 题目地址:https://leetcode ...
- 【LeetCode】71. Simplify Path 解题报告(Python)
[LeetCode]71. Simplify Path 解题报告(Python) 标签(空格分隔): LeetCode 作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://f ...
- matplotlib 进阶之Artist tutorial(如何操作Atrist和定制)
目录 基本 plt.figure() fig.add_axes() ax.lines set_xlabel 一个完整的例子 定制你的对象 obj.set(alpha=0.5, zorder=2), o ...
- 台湾旺玖MA8601|USB HUB方案|MA8601测试版
MA8601是USB 2.0高速4端口集线器控制器的高性能解决方案,完全符合通用串行总线规范2.0.MA8601继承了先进的串行接口技术,当4个DS(下游)端口同时工作时,功耗最低. MA8601采用 ...