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");
}
}

最新文章

  1. js-格式化数字保留两位小数-带千分符
  2. windows系统调用 利用事件对象实现进程通信
  3. opencv学习笔记(五)镜像对称
  4. c# HttpWebRequest与HttpWebResponse(转)
  5. SQL Server 存储过程之基础知识(转)
  6. Eclipse新建Android工程,在模拟器运行的时候提示Unfortunately,XXX has stopped.
  7. struts详细解释拦截器
  8. 【JDK1.8】Java 8源码阅读汇总
  9. JAVA编程入门
  10. SpringBoot ( 七 ) :springboot + mybatis 多数据源最简解决方案
  11. python--继承--方法的重写---和父类的扩展
  12. 解决mysql大小写敏感问题
  13. html中用href 实现点击链接弹出文件下载对话框
  14. anaconda python no module named &#39;past&#39;的解决方法
  15. [hdu6148][Valley Numer]
  16. 数字转换大写人民币的delphi实现
  17. 现场故障-数据量超出plsql developer结果集导致应用程序无数据现象
  18. NSDictionary打印编码改中文的方法
  19. jetty9优化的两处地方
  20. MongoDB固定集合(capped collection)

热门文章

  1. go等待N个线程完成操作总结
  2. hadoop集群无法找到datanode节点问题解决
  3. 深入理解redis数据类型
  4. Java编译与反编译
  5. 各个模式的accesstoken续期详解
  6. WPF里ItemsControl的分组实现 --listbox 实现分组
  7. C# winform自动更新 (附 demo下载)
  8. python基础学习(七)列表
  9. Windows驱动匹配详解
  10. js高级:event,事件冒泡,事件捕获