库函数中字符串函数的模拟实现

库函数的模拟实现
1.实现strcpy
在使用assert来检查传参时,应该包含头文件#include<assert.h>
注意事项:
1.源字符串的大小一定要小于等于目标字符串的大小,否则会出现内容越界的问题
2.在使用strcpy的时候也拷贝了'\0'
3.在自己实现MyStrcpy时要注意判定检查传参是否为空
4.strcpy是返回一个指针类型
 1 #include<stdio.h>
 2 #include<stdlib.h>
 3 #include<assert.h>
 4 char* Strcpy(char* destination, const char* source)
 5 {
 6  assert(destination != NULL);
 7  assert(source != NULL);
 8  int i = 0;
 9  while (source[i] != '\0')
10  {
11   destination[i] = source[i];
12   ++i;
13  }
14  destination[i] = '\0';
15  return destination;
16 }
17 int main()
18 {
19  char str[] = "hehe";
20  Strcpy(str, "haha");
21  printf("%s\n", str);
22  return 0;
23 }

 


2.实现strcat
在使用assert来检查传参时,应该包含头文件#include<assert.h>
注意事项:
1.源字符串的大小一定要小于等于目标字符串的大小,否则会出现内容越界的问题
2.在使用strcat的时候也拷贝了'\0'
3.在自己实现MyStrcat时要注意判定检查传参是否为空
4.strcat是返回一个指针类型
 1 #include<stdio.h>
 2 #include<stdlib.h>
 3 #include<assert.h>
 4 char* Strcat(char* destination, const char* source)
 5 {
 6  assert(destination != NULL);
 7  assert(source != NULL);
 8  int i = 0;
 9  while (destination[i]!='\0')
10  {
11   i++;
12  }
13  for (int j = 0; source[j] != '\0'; j++,i++)
14  {
15   destination[i] = source[j];
16  }
17  destination[i] = '\0';
18  return destination;
19 }
20 int main()
21 {
22  char str[1024] = "hehe";
23  Strcat(str, "haha");
24  printf("%s",str);
25  return 0;
26 }

 


3.实现strstr
1.strstr(str1,str2)的返回值为一个指针(比较str1中是否包含了str2)
2.返回的指针式指向了str1,
3.如果str2不是str1的子串,则返回0
 1 #include<stdio.h>
 2 #include<assert.h>
 3 
 4 const char* Strstr(const char* str1, const char* str2)
 5 {
 6  //校验合法性:
 7  //空指针和空字符串是不一样的
 8  
 9  //校验空指针 NULLL
10  assert(str1 != NULL);
11  assert(str2 != NULL);
12  //校验空字符串 " "
13  assert(*str1 != '\0');
14  assert(*str2 != '\0');
15  char* temp = str1;
16  const char* head_str = str1;
17  const char* sub_str = str2;
18  while (*head_str != '\0')
19  {
20   //char str1[] = "abcdacdefh";
21   //char str2[] = "cdef";
22   if (*head_str == *sub_str)
23   {
24    head_str++;
25    sub_str++;
26   }
27   else if (*sub_str == '\0')
28   {
29    return temp;
30   }
31   else if(*head_str != *sub_str)
32   {
33    temp++;
34    head_str = temp;
35    sub_str = str2;
36   }
37  }
38  return NULL;
39 }
40 int main()
41 {
42  char str1[] = "abcdacdefh";
43  char str2[] = "acdef";
44  char* ret = Strstr(str1, str2);
45  printf("%p\n", str1);
46  printf("%p\n", str2);
47  printf("%p\n", ret);
48  return 0;
49 }

 

 
4.实现strchr
strchr是在参数str所指向的字符串中搜索第一次出现字符的位置,找到返回地址,没有找到返回0
1.返回的是指针类型
2.没有找到就返回NULL
 1 #include <stdio.h>
 2 #include <string.h>
 3 #include<assert.h>
 4 char* Strchr(const char* str, int character)
 5 {
 6  assert(NULL != str);        
 7  while (*str && (*str != (char)character))
 8  {
 9   ++str;
10  }
11  if ((char)character == *str)//包含了*str和c都为0的情况。  
12  {
13   return (char *)str;
14  }
15  return NULL;  
16 }
17 int main()
18 {
19  char str[] = "This is a sample string";
20  char * pch;
21  pch = Strchr(str, 's');
22  while (pch != NULL)
23  {
24   printf("found at %d\n", pch - str + 1);
25   pch = Strchr(pch + 1, 's');
26  }
27  return 0;
28 }

 


5.实现strcmp
模拟实现strcmp
 1 #include<stdio.h>
 2 #include<assert.h>
 3 int Strcmp(const char* str1, const char* str2)
 4 {
 5  //合法性校验
 6  assert(str1 != NULL);
 7  assert(str2 != NULL);
 8  while (*str1 != '\0'&&*str2 != '\0')
 9  {
10   if (*str1 > *str2)
11   {
12    return 1;
13   }
14   else if (*str1 < *str2)
15   {
16    return -1;
17   }
18   else
19   {
20    str1++;
21    str2++;
22   }
23  }
24  //循环终止的原因:
25  //1.*str1=='\0';
26  //2.*str2=='\0';
27  //3.它两都等于‘\0’
28  if (*str1 < *str2)
29  {
30   return -1;
31  }
32  else if (*str1 > *str2)
33  {
34   return 1;
35  }
36  else
37  {
38   return 0;
39  }
40 }
41 //strcmp(str1,str2)相等返回0,str1<str2,返回一个负数,str1>str2,返回一个正数。
42 int main()
43 {
44  char str1[] = "hehe";
45  char str2[] = "hehehe";
46  int ret = Strcmp(str1, str2);
47  if (ret == 0)
48  {
49   printf("str1==str2\n");
50  }
51  else if (ret < 0)
52  {
53   printf("str1<str2");
54  }
55  else
56  {
57   printf("str1>str2");
58  }
59  return 0;
60 }

 

posted on 2019-03-01 12:12  The_Ocean  阅读(170)  评论(0编辑  收藏  举报

导航