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. 【原创】STM32下波特率计算详解

    波特率的计算 STM32下的波特率和串口外设时钟息息相关,USART 1的时钟来源于APB2,USART 2-5的时钟来源于APB1.在STM32中,有个波特率寄存器USART_BRR,如下:     ...

  2. SOAP系列目录

    1.协议分析 2.WebService.WCF介绍 3.HttpClientHelper实现webservice调用 4.SoapCore介绍

  3. 【春华秋实】.NET Framework与.NET Core

    C#是微软发布的面向对象的,运行与.NET Framework之上的高级程序设计语言,属于编译型语言,也是目前.NET Framework的主角.C#语言语法简单而优雅,同时也有着很高的开发效率,尤其 ...

  4. Web前端课程设计——个人主页

    大三上学期期末总结,嗯,没错,是上学期,写在新学期开始hhh. 上学期学了一门HTML5+CSS3的课程,也叫Web前端技术,期末的课程设计是写一个个人主页,能够在浏览器中打开的静态网页.通过一学期的 ...

  5. Python循环结构用法

    本文介绍python中的while循环.for循环.在python中for可以用于循环,也可用于另一种近亲的列表解析,列表解析是python中非常重要的特性,详细内容见后面的文章. 一般来说,pyth ...

  6. 部署vmware-vcsa 6.5

    介绍一下vcsa vsphere的两个最重要的组件是esxi和vcenter server,esxi是虚拟化主机管理软件,而vcenter server则是管理.组织多台esxi主机的管理中心. es ...

  7. 【转载】网站遭遇DDoS攻击怎么办

    在网站运维过程中,有些人的网站遭遇过DDoS攻击,DDos攻击又叫做分布式拒绝服务攻击.DDos攻击将多个计算机联合起来作为攻击平台,对一个或多个目标发动DDoS攻击,从而成倍地提高拒绝服务攻击的威力 ...

  8. Web部分_2

    详细描述MVC 基于Java的Web应用系统采用MVC架构模式,即model(模型).view(视图).controller(控制)分离设计:这是目前Web应用服务系统的主流设计方向. Model:即 ...

  9. vs2010 编译平台 X86 X64 anycpu

    X86既32位程序,X64既64位程序,anycpu会根据当前的操作系统位数决定 但是如果应用程序编译成anycpu,会由操作系统位数决定,如果是dll之类的,会由调用dll的主程序位数决定 所以一般 ...

  10. [Linux] 简单安装和使用composer

    wget https://getcomposer.org/installer //下载一个脚本文件 php installer //php执行下这个php脚本 mv composer.phar /us ...