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. Centos-7修改yum源为国内的yum源

    以centos7为例 ,以 修改为阿里的yum源 1. 备份本地yum源 [root@localhost yum.repos.d]# cp CentOS-Base.repo CentOS-Base.r ...

  2. 翻译:如何向MariaDB中快速插入数据(已提交到MariaDB官方手册)

    本文为mariadb官方手册:How to Quickly Insert Data Into MariaDB的译文. 原文:https://mariadb.com/kb/en/how-to-quick ...

  3. JavaScript 系列博客(四)

    JavaScript 系列博客之(四) 前言 本篇介绍 JavaScript 中的对象.在第一篇博客中已经说到 JavaScript 是一种''对象模型''语言.所以可以这样说,对象是 JavaScr ...

  4. SQLite与FMDB使用中区别

    前几篇已经写完了SQLite与FMDB的基本内容以及衍生出来的知识点,我们这一篇主要讲述FMDB与SQLite在基本使用中的区别,大约需要5-10分钟时间讲述内容,欢迎大家指正. 基本使用区别 1.数 ...

  5. javascript小实例,实现99乘法表及隔行变色

    人生短暂,废话不多说,直奔主题! 这个小实例的要求: 实现在页面中输出99乘法表.(要求:以每三行为一组,实现隔行变色(颜色为白,红,黄(也可自己定义)),鼠标滑过每一行,行背景颜色变为蓝色,鼠标离开 ...

  6. [转]bitcoin API reference (JSON-RPC)

    本文转自:https://en.bitcoin.it/wiki/API_reference_%28JSON-RPC%29#Node.js API reference (JSON-RPC)     Co ...

  7. 第一册:lesson nineteen。

    原文:tired and thirsty. A:What's the matter,children? B:We are tired and thirsty. A:Sit down,here. Are ...

  8. 第一册:lesson 11.

    原文:Is this your shirt? A:Whose shirt is that? Is this your shirt B? B:NO,sir. It's not my shirt.This ...

  9. .net core Identity集成IdentityServer(3) 一键登出

    在客户端程序, 我们补充一键登出操作. 使用了idsv之后, 退出的操作需要删除本地cookie, 然后去请求认证服务器, 也删除认证服务器的cookie. 官网给的退出的代码 public asyn ...

  10. .Net NPOI 根据excel模板导出excel、直接生成excel

    一.根据Excel模板导出excel 1.导入NPOI.dll  2.DAL中添加类ExportExcel.cs using NPOI.SS.UserModel; using System; usin ...