自己实现字符串操作函数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 ...
随机推荐
- HDU 4639 Hehe(字符串处理,斐波纳契数列,找规律)
题目 //每次for循环的时候总是会忘记最后一段,真是白痴.... //连续的he的个数 种数 //0 1 //1 1 //2 2 //3 3 //4 5 //5 8 //…… …… //斐波纳契数列 ...
- POJ 2010
Moo University - Financial Aid Time Limit: 1000MS Memory Limit: 30000K Total Submissions: 4235 A ...
- jQuery实现表格隔行换色且感应鼠标高亮行变色
jQuery插件实现表格隔行换色且感应鼠标高亮行变色 http://www.jb51.net/article/41568.htm jquery 操作DOM的基本用法分享http://www.jb51. ...
- POJ 3525 Most Distant Point from the Sea (半平面交向内推进+二分半径)
题目链接 题意 : 给你一个多边形,问你里边能够盛的下的最大的圆的半径是多少. 思路 :先二分半径r,半平面交向内推进r.模板题 #include <stdio.h> #include & ...
- 2014多校第五场1010 || HDU 4920 Matrix multiplication(矩阵乘法优化)
题目链接 题意 : 给你两个n*n的矩阵,然后两个相乘得出结果是多少. 思路 :一开始因为知道会超时所以没敢用最普通的方法做,所以一直在想要怎么处理,没想到鹏哥告诉我们后台数据是随机跑的,所以极端数据 ...
- hdu 4389 X mod f(x) 数位DP
思路: 每次枚举数字和也就是取模的f(x),这样方便计算. 其他就是基本的数位Dp了. 代码如下: #include<iostream> #include<stdio.h> # ...
- ubuntu为Python添加默认搜索路径
我们在自己写python模块的时候,怎么样把自己写的模块加入到python默认就有的搜索路径中呢?不要每次非得import sys; sys.path.append(‘/home/uestc/rese ...
- for (Map.Entry<Long, Integer> me : zlSendMap.entrySet())
public static void main(String[] args) throws IOException { Map<String,String> map = new HashM ...
- FreePascal经典资料
------------------------------------------------------------------------ 这是每个版本的changelog: http://bu ...
- linux shell 命令学习(3) split - split a file into pieces
split 用来进行文件分割的指令 split [OPTION]... [INPUT [PREFIX]] 发现这个命令是因为有个需求,有个10W行的文本文件,需要分成5个2w行的文本文件, 查了一下资 ...