自己实现字符串操作函数strlen(),strcat(),strcpy(),strcmp()
1.strlen()函数是求解字符串的有效长度的
1)非递归实现
size_t my_strlen(const char *str)
{
assert(str != NULL); //断言,保证指针参数不能为空
size_t count = 0;
const char *pstr = str; //参数保护
while (*pstr++ != '\0')
{
count++;
}
return count;
}
2)非递归实现
size_t my_strlen(const char *str)
{
assert(str != NULL); //断言,保证指针参数不能为空
const char *pstr = str; //参数保护
if (*str == NULL)
return 0;
else
return my_strlen(str + 1) + 1;
}
2.strcat()字符串连接函数
char* my_strcat(char *str1,const char* str2)
{
assert(str1 != NULL&&str2!=NULL);
char *pstr1 = str1;
const char* pstr2 = str2;
while(*pstr1 != '\0')
{
pstr1++;
}
while (*pstr2!= '\0')
{
*pstr1++ = *pstr2++;
}
*pstr1 = '\0';
return str1;
}
3.strcpy()字符串拷贝函数
char* my_strcpy(char *str1,const char* str2)
{
assert(str1 != NULL&&str2!=NULL);
char *pstr1 = str1;
const char* pstr2 = str2;
while (*pstr2!= '\0')
{
*pstr1++ = *pstr2++;
}
*pstr1 = '\0';
return str1;
}
4.strcmp()字符串比较函数
int my_strcmp(const char *str1,const char* str2)
{
assert(str1 != NULL&&str2!=NULL);
const char *pstr1 = str1;
const char* pstr2 = str2;
while (*pstr2!= '\0'&&*pstr2!='\0')
{
if (*pstr1 > *pstr2)
return 1;
else if (*pstr1 < *pstr2)
return -1;
pstr1++;
pstr2++;
}
while (*pstr1 != '\0'&&*pstr2 == '\0')
return 1;
while (*pstr1 == '\0'&&*pstr2 != '\0')
return -1;
return 0;
}
以上函数虽然可以实现字符串的拷贝函数,但是能不能做的更好呢?下面给出另一种实现方法:
int my_strcmp(const char *str1,const char* str2)
{
assert(str1 != NULL&&str2!=NULL);
const char *pstr1 = str1;
const char* pstr2 = str2;
int result = 0;
while (*pstr2!= '\0'||*pstr2!='\0')
{
result = *pstr1 - *pstr2;
if (result != 0)
break;
pstr1++;
pstr2++;
}
if (result>0)
return 1;
else if (result<0)
return -1;
return result;
}
自己实现字符串操作函数strlen(),strcat(),strcpy(),strcmp()的更多相关文章
- strlen strcat strcpy strcmp 自己实现
strlen strcat strcpy strcmp 自己实现 strlen include <stdio.h> #include <string.h> #include & ...
- 转:C语言字符串操作函数 - strcpy、strcmp、strcat、反转、回文
转自:C语言字符串操作函数 - strcpy.strcmp.strcat.反转.回文 C++常用库函数atoi,itoa,strcpy,strcmp的实现 作者:jcsu C语言字符串操作函数 1. ...
- C语言字符串操作函数 - strcpy、strcmp、strcat、反转、回文
原文:http://www.cnblogs.com/JCSU/articles/1305401.html C语言字符串操作函数 1. 字符串反转 - strRev2. 字符串复制 - strcpy3. ...
- C语言的常用字符串操作函数(一)
一直做的是单片机相关的程序设计,所以程序设计上更偏向底层,对于字符串的操作也仅限于液晶屏幕上的显示等工作,想提高下字符串操作的水平,而不是笨拙的数组替换等方式,翻看帖子发现C语言的字符串操作函数竟然这 ...
- LoadRunner中常用的字符串操作函数
LoadRunner中常用的字符串操作函数有: strcpy(destination_string, source_string); strc ...
- [转载]c++常用字符串操作函数
原文地址:c++常用字符串操作函数作者:Valsun 函数名: stpcpy 功 能: 拷贝一个字符串到另一个 用 法: char *stpcpy(char *destin, char *source ...
- JavaScript中常见的字符串操作函数及用法
JavaScript中常见的字符串操作函数及用法 最近几次参加前端实习生招聘的笔试,发现很多笔试题都会考到字符串的处理,比方说去哪儿网笔试题.淘宝的笔试题等.如果你经常参加笔试或者也是一个过来人,相信 ...
- linux makefile字符串操作函数 替换subst、模式替换patsubst、去首尾空格strip、查找字符串findstring、过滤filter、反过滤filter-out、排序函数sort、取单词word、取单词串wordlist、个数统计words
1.1 字符操作函数使用 在Makefile中可以使用函数来处理变量,从而让我们的命令或是规则更为的灵活和具有智能.make所支持的函数也不算很多,不过已经足够我们的操作了.函数调用后,函 ...
- mysql常用字符串操作函数大全,以及实例
今天在论坛中看到一个关于mysql的问题,问题如下 good_id cat_id12654 665,56912655 601,4722 goods_id是商品i ...
随机推荐
- Dungeon Game
The demons had captured the princess (P) and imprisoned her in the bottom-right corner of a dungeon. ...
- HDU 4027 Can you answer these queries?(线段树,区间更新,区间查询)
题目 线段树 简单题意: 区间(单点?)更新,区间求和 更新是区间内的数开根号并向下取整 这道题不用延迟操作 //注意: //1:查询时的区间端点可能前面的比后面的大: //2:优化:因为每次更新都 ...
- HDU 1098 Ignatius's puzzle(数学归纳)
以下引用自http://acm.hdu.edu.cn/discuss/problem/post/reply.php?postid=8466&messageid=2&deep=1 题意以 ...
- 后缀树系列一:概念以及实现原理( the Ukkonen algorithm)
首先说明一下后缀树系列一共会有三篇文章,本文先介绍基本概念以及如何线性时间内构件后缀树,第二篇文章会详细介绍怎么实现后缀树(包含实现代码),第三篇会着重谈一谈后缀树的应用. 本文分为三个部分, 首先介 ...
- SGU 107
107. 987654321 problem time limit per test: 0.25 sec. memory limit per test: 4096 KB For given numbe ...
- iOS验证码倒计时(GCD实现)
+ (void)verificationCode:(void(^)())blockYes blockNo:(void(^)(id time))blockNo { __block ; //倒计时时间 d ...
- Android OpenGL 学习笔记 --开始篇
转自: http://www.cnblogs.com/TerryBlog/archive/2010/07/09/1774475.html 1.什么是 OpenGL? OpenGL 是个专业的3D程序接 ...
- GDB下查看内存命令(x命令)
http://blog.csdn.net/allenlinrui/article/details/5964046 可以使用examine命令(简写是x)来查看内存地址中的值.x命令的语法如下所示: x ...
- Java多线程-线程的调度(守护线程)
本文转自http://www.cnblogs.com/linjiqin/p/3210004.html 感谢作者 守护线程与普通线程写法上基本没啥区别,调用线程对象的方法setDaemon(true), ...
- 一个国外网盘pCloud——支持离线下载
给大家分享一个国外网盘<支持离线下载> https://my.pcloud.com/#page=register&invite=HiegZ8aBrt7