1. 程式人生 > >庫函數中字符串函數的模擬實現

庫函數中字符串函數的模擬實現

while span 情況 負數 main clu null tdi 使用strcpy

庫函數的模擬實現
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* destnation, const
char* source) 5 { 6 assert(destnation != NULL); 7 assert(source != NULL); 8 int i = 0; 9 while (source[i] != \0) 10 { 11 destnation[i] = source[i]; 12 ++i; 13 } 14 destnation[i] = \0; 15 return destnation; 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* destantion, const char* source) 5 { 6 assert(destantion != NULL); 7 assert(source != NULL); 8 int i = 0; 9 while (destantion[i]!=\0) 10 { 11 i++; 12 } 13 for (int j = 0; source[j] != \0; j++,i++) 14 { 15 destantion[i] = source[j]; 16 } 17 destantion[i] = \0; 18 return destantion; 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 }

庫函數中字符串函數的模擬實現