strlen strcat strcpy strcmp 自己实现

strlen

include <stdio.h>
#include <string.h>
#include <assert.h> size_t my_strlen(const char* str){
assert(str != NULL); const char *tmp = str; size_t count = 0;
while(*tmp++ != '\0'){
count++;
}
return count;
} size_t my_strlen1(const char* str){
assert(str != NULL);
if(*str == 0){
return 0;
}else{
return my_strlen1(str+1) + 1;
}
}
int main(){
char as[] = "hello C";
printf("%ld\n",strlen(as));
printf("%ld\n",my_strlen(as));
printf("%ld\n",my_strlen1(as));
}

strcat

#include <stdio.h>
#include <string.h>
#include <assert.h>
#include <malloc.h> char* my_strcat(char* strd, const char* strs){
assert(strd != NULL && strs != NULL);
char *tmp = strd;
while(*tmp++ != 0){}
tmp--;
while(*strs != 0){
*(tmp++) = *strs++;
}
*tmp = '\0';
return strd;
} int main(){
char s1[20] = "hello";
char s2[] = " C";
printf("strcat before s1 = %s\n", s1);
char *str = my_strcat(s1,s2);
printf("strcat after s1 = %s\n", s1);
printf("strcat after str = %s\n", str);
}

strcpy

#include <stdio.h>
#include <string.h>
#include <assert.h>
#include <malloc.h> char* my_strcpy(char* strd, const char* strs){
assert(NULL != strd && NULL != strs);
char* tmp = strd;
while(*strs != '\0'){
*tmp++ = *strs++;
}
*tmp = '\0';
return strd;
}
int main(){
char s1[20] = "hello";
char s2[] = " wod";
printf("strcpy before s1 = [%s]\n", s1);
char *str = my_strcpy(s1,s2);
printf("strcpy after s1 = [%s]\n", s1);
printf("strcat after str = [%s]\n", str);
}

strcmp

#include <stdio.h>
#include <string.h>
#include <assert.h>
#include <malloc.h> int my_strcmp(const char *s1, const char *s2){ assert(NULL != s1 && NULL != s2);
int res = 0;
while(*s1 != '\0' || *s2 != '\0'){
if(*s1 > *s2){
res = 1;
break;
}else if(*s1 < *s2){
res = -1;
break;
}else{
s1++;
s2++;
}
}
return res;
} int main(){
char *s1 = "a1123";
char *s2 = "a1123";
int res = my_strcmp(s1, s2);
if(res == 0){
printf("s1 == s2\n");
}else if(res > 0){
printf("s1 > s2\n");
}else{
printf("s1 < s2\n");
}
}

strlen strcat strcpy strcmp 自己实现的更多相关文章

  1. 自己实现字符串操作函数strlen(),strcat(),strcpy(),strcmp()

    1.strlen()函数是求解字符串的有效长度的 1)非递归实现 size_t my_strlen(const char *str) { assert(str != NULL);  //断言,保证指针 ...

  2. 实现字符串函数,strlen(),strcpy(),strcmp(),strcat()

    实现字符串函数,strlen(),strcpy(),strcmp(),strcat() #include<stdio.h> #include<stdlib.h> int my_ ...

  3. 不使用库函数、自己编写的(strlen、strcpy、strcmp、strcat、memcmp、memcpy、memmove)

    不使用库函数.自己编写的(strlen.strcpy.strcmp.strcat.memcmp.memcpy.memmove) //求字符串长度的函数 int my_strlen(const char ...

  4. 转载 C++常用库函数atoi,itoa,strcpy,strcmp的实现

    C++常用库函数atoi,itoa,strcpy,strcmp的实现 C语言字符串操作函数 1. 字符串反转 - strRev2. 字符串复制 - strcpy3. 字符串转化为整数 - atoi4. ...

  5. strlen() 和 strcpy()函数

    strlen() 和 strcpy()函数的区别,这两个一个是返回一个C风格字符串的长度,一个是对一个C风格字符串的拷贝,两个本来功能上是不同的,此外,他们还有一些细小的区别:strlen(" ...

  6. C语言简单strcat和strcmp的实现

    对于C标准库中的字符串处理函数应该平常用的比较多:简单实现strcat和strcmp _strcpy: char *_strcpy(char *dest, char *src) { char *buf ...

  7. strcpy/strlen/strcat/strcmp面试总结

    <strcpy拷贝越界问题> 一. 程序一 #include<stdio.h> #include<string.h> void main() { char s[]= ...

  8. C语言中strcpy,strcmp,strlen,strcat函数原型

    //strcat(dest,src)把src所指字符串添加到dest结尾处(覆盖dest结尾处的'\0')并添加'\0' char *strcat(char * strDest, const char ...

  9. strcpy,strlen, strcat, strcmp函数,strlen函数和sizeof的区别

    //计算字符串实际长度        //strlen()函数:当遇到'\0'时,计算结束,'\0'不计入长度之内,如果你只定义没有给它赋初值,这个结果是不定的,它会从首地址一直找下去,直到遇到'\0 ...

随机推荐

  1. 项目实战4—HAProxy实现高级负载均衡实战和ACL控制

     haproxy实现高级负载均衡实战 环境:随着公司业务的发展,公司负载均衡服务已经实现四层负载均衡,但业务的复杂程度提升,公司要求把mobile手机站点作为单独的服务提供,不在和pc站点一起提供服务 ...

  2. Http系列目录

    1.Http简史 2.Http协议基本术语 3.Http1.1 4.Http2.0

  3. RabbitMQ系列(一)RabbitMQ在Ubuntu上的环境搭建

    环境配置 Ubuntu Server 18.04 RabbitMQ 3.6.10 安装之前 我们使用apt-get进行RabbitMQ安装,在安装之前,强烈建议您把apt源换位国内,大大增加下载安装的 ...

  4. HDFS简单测试

    使用Hadoop的Java客户端API操作分布式文件系统#获取文件系统实现//hdfs://master01:9000/FileSystem get(URI uri[,Configuration co ...

  5. 第一册:lesson eighty nine.

    原文: For sale. A:Good afternoon. I believe that the house is for sale. B:That's right. A:May I have a ...

  6. 【转】ADO.Net对Oracle数据库的操作

    一 ADO.Net简介 [转自网络,收藏学习] 访问数据库的技术有许多,常见的有一下几种:开放数据库互联(ODBC). 数据访问对象(DAO).远程数据对象(RDO). ActiveX数据对象(ADO ...

  7. .NET页面导出Excel

    public static void CreateExcel(DataSet ds)        {            string filename = DateTime.Now.ToStri ...

  8. 第五讲 smart qq poll包处理 以及 私聊 群聊消息收发

    发送 poll包 public static void Login_PostPoll() { try { string url = "http://d1.web2.qq.com/channe ...

  9. frp 初探

    条件: (1) 服务器端要有公网 IP (2) 客户端能上网,能够访问服务器的公网 IP 下载 https://github.com/fatedier/frp/releases 根据服务器和客户端的操 ...

  10. Spring,SpringMvc配置常见的坑,注解的使用注意事项,applicationContext.xml和spring.mvc.xml配置注意事项,spring中的事务失效,事务不回滚原因

    1.Spring中的applicationContext.xml配置错误导致的异常 异常信息: org.apache.ibatis.binding.BindingException: Invalid ...