目录
一、strlen函数---求长度
(一)函数介绍
(二)举例
(三) 模拟实现
1. 计数器
2. 递归
二、strcpy函数---拷贝
(一) 函数介绍
(二)举例
(三)模拟实现
三、strcat函数---追加
(一)函数介绍
(二) 举例
(三)模拟实现
四、strcmp函数---比较
(一)函数介绍
(二)举例
(三)模拟实现
五、strncpy函数---拷贝
(一)函数介绍
(二)举例
六、strncat函数---追加
(一)函数介绍
(二)举例
七、strncmp函数---比较
(一)函数介绍
(二)举例
八、总结
#define _CRT_SECURE_NO_WARNINGS 1
#include
#include
#include//strlen模拟实现
size_t Strlen(const char* str)
{assert(str);int count = 0;while (*str != '\0'){count++;str++;}return count;
}int main()
{char a[] = "abcdefg";int len = Strlen(a);printf("%d", len);return 0;
}
#define _CRT_SECURE_NO_WARNINGS 1
#include
#include
#includesize_t Strlen(const char* str)
{assert(str);int count = 0;if (*str != 0){count = 1 + Strlen(str + 1);}elsereturn 0;
}int main()
{char a[] = "abcdefg";int len = Strlen(a);printf("%d", len);return 0;
}
int main()
{char arr1[] = "abcdefg";char arr2[] = "hij";strcpy(arr1, arr2);printf("%s", arr1);return 0;
}
#define _CRT_SECURE_NO_WARNINGS 1
#include
#include
#include
char* Strcpy(char* dest, const char* src)
{char* ret = dest;assert(dest && src);while (*dest++ = *src++){;}return ret;
}int main()
{char arr1[] = "abcdefg";char arr2[] = "hijk";char* p = Strcpy(arr1, arr2);printf("%s", p);return 0;
}
#define _CRT_SECURE_NO_WARNINGS 1
#include
#include
#include
char* Strcat(char* dest, const char* src)
{char* ret = dest;assert(dest && src);
//找目标空间中的'\0'while (*dest != '\0'){dest++;}
//拷贝while (*dest++ = *src++){;}return ret;
}
int main()
{char arr1[20] = "abcd ";char arr2[] = "ef";Strcat(arr1, arr2);printf("%s", arr1);return 0;
}
#define _CRT_SECURE_NO_WARNINGS 1
#include
#include
#include
int Strcmp(const char* s1, const char* s2)
{assert(s1 && s2);while (*s1 == *s2){if (*s1 == '\0') //字符串全部比较完毕return 0;s1++;s2++;}return *s1 - *s2;
}
int main()
{char arr1[] = "abcd ";char arr2[] = "abf";int x = Strcmp(arr1, arr2);printf("%d", x);return 0;
}
char *strncpy( char *strDest, const char *strSource, size_t count );
char *strncat( char *strDest, const char *strSource, size_t count );
int strncmp( const char *string1, const char *string2, size_t count );
#define _CRT_SECURE_NO_WARNINGS 1
#include
#include
#include
int main()
{char arr1[] = "abcdefg";char arr2[] = "abc";int ret1 = strncmp(arr1, arr2, 2);int ret2 = strncmp(arr1, arr2, 4);printf("%d\n", ret1);//0printf("%d\n", ret2);//1return 0;
}
#define _CRT_SECURE_NO_WARNINGS 1
#include
#include
#include
int main()
{char arr1[] = "abc";char arr2[] = "abc";int ret1 = strncmp(arr1, arr2, 7);printf("%d\n", ret1);//0return 0;
}
’