字符串、内存函数的介绍(13)

目录

1、字符串函数

1、strlen

模拟实现:

2、strcpy

模拟实现:

3、strcat

模拟实现:

4、strcmp

模拟实现:

5、strncpy

6、strncat

7、strncmp

8、strstr

模拟实现:

9、strtok

10、strerror

11、其他字符函数

2、内存函数

1、memcpy

模拟实现:

2、memmove

模拟实现:

3、memcmp

4、memset 


1、字符串函数

1、strlen

strlen 是用于求字符串长度的,统计的是字符串中 \0 之前出现的字符个数。

注意:

1、字符串用 '\0' 作为结束标志,strlen函数返回的是在字符串中 '\0' 前面出现的字符个数(不包含 '\0' )。

2、参数指向的字符串必须要以 '\0' 结束。

3、注意函数的返回值为size_t,是无符号的。

模拟实现:

方式一:计数器方式

//计数器方式
int my_strlen(const char * str)
{
   int count = 0;
   while(*str)
   {
      count++;
      str++;
   }
   return count;
}

方式二:不能创建临时变量计数器

//不能创建临时变量计数器
int my_strlen(const char * str)
{
   if(*str == '\0')
       return 0;
   else
       return 1+my_strlen(str+1);
}

方式三:指针-指针的方式

int my_strlen(char *s)
{
    char *p = s;
    while(*p != ‘\0’ )
       p++;
    return p-s;
}

2、strcpy

strcpy 字符串拷贝,将源拷贝到目的地,包含\0。

注意:

1、Copies the C string pointed by source into the array pointed by destination, including the terminating null character (and stopping at that point).

2、源字符串必须以 '\0' 结束。

3、会将源字符串中的 '\0' 拷贝到目标空间。

4、目标空间必须足够大,以确保能存放源字符串。

模拟实现:

char* my_strcpy(char* dest, const char* src)
{
	char* ret = dest;
	assert(dest != NULL);
	assert(src != NULL);
	while ((*dest++ = *src++))
	{
		;
	}
	return ret;
}

3、strcat

strcat 字符串追加。

注意:

1、Appends a copy of the source string to the destination string. The terminating null character in destination is overwritten by the first character of source, and a null-character is included at the end of the new string formed by the concatenation of both in destination.

2、源字符串必须以 '\0' 结束。

3、目标空间必须有足够的大,能容纳下源字符串的内容。

4、目标空间必须可修改。

模拟实现:

char* my_strcat(char* dest, const char* src)
{
	char* ret = dest;
	assert(dest != NULL);
	assert(src != NULL);
	while (*dest)
	{
		dest++;
	}
	while ((*dest++ = *src++))
	{
		;
	}
	return ret;
}

4、strcmp

strcmp 字符串比较,比较对应位置的ASCII码值大小。

说明:

1、This function starts comparing the first character of each string. If they are equal to each other, it continues with the following pairs until the characters differ or until a terminating null-character is reached.

2、第一个字符串大于第二个字符串,则返回大于0的数字。

3、第一个字符串等于第二个字符串,则返回0。

4、第一个字符串小于第二个字符串,则返回小于0的数字。

模拟实现:

int my_strcmp(const char* src, const char* dst)
{
    int ret = 0;
    assert(src != NULL);
    assert(dst != NULL);
    while (!(ret = *(unsigned char*)src - *(unsigned char*)dst) && *dst)
        ++src, ++dst;
    if (ret < 0)
        ret = -1;
    else if (ret > 0)
        ret = 1;
    return(ret);
}

5、strncpy

strncpy 拷贝n个字符从源到目的地,不够补0。

注意:

1、Copies the first num characters of source to destination. If the end of the source C string (which is signaled by a null-character) is found before num characters have been copied, destination is padded with zeros until a total of num characters have been written to it.

2、拷贝num个字符从源字符串到目标空间。

3、如果源字符串的长度小于num,则拷贝完源字符串之后,在目标的后边追加0,直到num个。

6、strncat

注意:

1、Appends the first num characters of source to destination, plus a terminating null-character.

2、If the length of the C string in source is less than num, only the content up to the terminating null-character is copied.

7、strncmp

注意:比较到出现字符不一样或者一个字符串结束或者num个字符全部比较完。

8、strstr

在一个字符串中查找另一个字符串是否存在,若存在返回第一次出现字串的起始位置;若不存在,则返回NULL。

注意:Returns a pointer to the first occurrence of str2 in str1, or a null pointer if str2 is not part of str1.

模拟实现:

char* strstr(const char* str1, const char* str2)
{
    char* cp = (char*)str1;
    char* s1, * s2;
    if (!*str2)
        return((char*)str1);
    while (*cp)
    {
        s1 = cp;
        s2 = (char*)str2;
        while (*s1 && *s2 && !(*s1 - *s2))
            s1++, s2++;
        if (!*s2)
            return(cp);
        cp++;
    }
    return(NULL);
}

9、strtok

strtok 字符串分割

注意:

1、sep参数是个字符串,定义了用作分隔符的字符集合。

2、第一个参数指定一个字符串,它包含了0个或者多个由sep字符串中一个或者多个分隔符分割的标记。

3、strtok函数找到str中的下一个标记,并将其用 \0 结尾,返回一个指向这个标记的指针。(注: strtok函数会改变被操作的字符串,所以在使用strtok函数切分的字符串一般都是临时拷贝的内容 并且可修改。)

4、strtok函数的第一个参数不为 NULL,函数将找到str中第一个标记,strtok函数将保存它在字符串中的位置。

5、strtok函数的第一个参数为 NULL,函数将在同一个字符串中被保存的位置开始,查找下一个标 记。

6、如果字符串中不存在更多的标记,则返回 NULL 指针。

10、strerror

strerror:将错误码转化为错误信息。

注意:

1、返回错误码,所对应的错误信息。

2、C语言中库函数报错时会返回一个错误码,错误码记录到错误码变量errno中。(errno是C语言提供的全局变量,头文件#include<errno.h>)

11、其他字符函数

如果函数参数符合下列条件就返回真。

字符转换:

int tolower ( int c );

int toupper ( int c );

2、内存函数

1、memcpy

注意:

1、函数memcpy从source的位置开始向后复制num个字节的数据到destination的内存位置。

2、这个函数在遇到 '\0' 的时候并不会停下来。

3、如果source和destination有任何的重叠,复制的结果都是未定义的。

模拟实现:

void* memcpy(void* dst, const void* src, size_t count)
{
    void* ret = dst;
    assert(dst);
    assert(src);
    while(count--) 
    {
        *(char*)dst = *(char*)src;
        dst = (char*)dst + 1;
        src = (char*)src + 1;
    }
    return(ret);
}

2、memmove

注意:

1、和memcpy的差别就是memmove函数处理的源内存块和目标内存块是可以重叠的。

2、如果源空间和目标空间出现重叠,就得使用memmove函数处理。

模拟实现:

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);
}

3、memcmp

注意:比较从ptr1和ptr2指针开始的num个字节。

4、memset 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值