#include<stdio.h>
#include<assert.h>
//求字符串函数
int MyStrlen(const char* str) {
int count = 0;
while (*str != '\0') {
count++;
str++;
}
return count;
}
//字符串拷贝
char* Mystrcpy(char* dst, const char* src) {
char* ret = dst;
while (*src != '\0') {
*dst = *src;
dst++;
src++;
}
return ret;
}
//字符串比较
int MyStrcmp(const char* dst, const char* src) {
int ret = 0;
while (!(ret = *(unsigned char*)src - *(unsigned char*)dst) && *dst)
++src; ++dst;
if (ret < 0)
ret = -1;
else if (ret > 0)
ret = 1;
return ret;
}
//字符串连接
char* MyStrcat(char* dst, const char* src)
{
char* ret = dst;
while (*dst)
{
dst++;
}
while (*src!='\0')
{
*dst = *src;
dst++, src++;
}
*dst = '\0';
return ret;
}
//找出str2在str1中第一次出现的位置
char* MyStrstr(const char* str1, const char* str2)
{
char* cp = (char*)str1;
char* substr = (char*)str2;
char* s1 = NULL;
if (*str2 == '\0')
return NULL;
while (*cp)
{
s1 = cp;
substr = str2;
while (*s1 && *substr && (*s1 == *substr))
{
s1++;
substr++;
}
if (*substr == '\0')
return cp;
cp++;
}
}
//由src所指内存区域复制count个字节到dst所指内存区域
void* memmove(void* dst, const void* src, size_t count)
{
void* ret = dst;
if (dst <= src || (char*)dst >= ((char*)src + count)) {
while (count--) {
*(char*)dst = *(char*)src;
dst = (char*)dst + 1;
src = (char*)src + 1;
}
}
else {
dst = (char*)dst + count - 1;
src = (char*)src + count - 1;
while (count--) {
*(char*)dst = *(char*)src;
dst = (char*)dst - 1;
src = (char*)src - 1;
}
}
return(ret);
}
//由src所指内存区域拷贝count个字节到dest所指内存区域
void* memcpy(void* dst, const void* src, size_t count)
{
void* ret = dst;
while (count--){
*(char*)dst = *(char*)src;
dst = (char*)dst + 1;
src = (char*)src + 1;
}
return(ret);
}
int main() {
const char* str = "abcd1234";
//printf("%d", MyStrlen(str));
char str1[16] = "0";
//Mystrcpy(str1, str);
//printf("%s", str1);
//printf("%d", MyStrcmp(str, str2));
const char* str2 = "1234";
char str3[20] = "ABCD1234";
//char* ret = MyStrcat(str3,str);
//printf("%s", ret);
//char* ret=MyStrstr(str,str2);
//printf("%s", ret);
char* ret = memcpy(str1, str,2);
printf("%s", ret);
return 0;
}
字符串操作函数的实现(C语言)
最新推荐文章于 2022-07-03 22:57:16 发布