zoukankan      html  css  js  c++  java
  • strlen strcat strcpy strcmp 自己实现

    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++ != ''){
        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
    ",strlen(as));
      printf("%ld
    ",my_strlen(as));
      printf("%ld
    ",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 = '';
      return strd;
    }
    
    int main(){
      char s1[20] = "hello";
      char s2[] = " C";
      printf("strcat before s1 = %s
    ", s1);
      char *str = my_strcat(s1,s2);
      printf("strcat after s1 = %s
    ", s1);
      printf("strcat after str = %s
    ", 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 != ''){
        *tmp++ = *strs++;
      }
      *tmp = '';
      return strd;
    }
    int main(){
      char s1[20] = "hello";
      char s2[] = " wod";
      printf("strcpy before s1 = [%s]
    ", s1);
      char *str = my_strcpy(s1,s2);
      printf("strcpy after s1 = [%s]
    ", s1);
      printf("strcat after str = [%s]
    ", 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 != '' || *s2 != ''){
        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
    ");
      }else if(res > 0){
        printf("s1 > s2
    ");
      }else{
        printf("s1 < s2
    ");
      }
    }
    
  • 相关阅读:
    无穷有界数列,必有收敛子列(待证)
    有界闭区间内的连续函数必然有界
    数学分析提纲目录
    有限覆盖定理
    函数极限的柯西收敛准则
    数列的柯西收敛准则证明-----华东师大构造数列证明法
    数列柯西收敛准则的子列收敛证明法(取自中科大数分教材)
    用有限覆盖证明闭区间上的连续函数,必然一致连续
    数据库-模糊查询-like关键字-练习
    数据库-基础查询-练习
  • 原文地址:https://www.cnblogs.com/xiaoshiwang/p/9175482.html
Copyright © 2011-2022 走看看