字符、字符串函数和内存操作函数


前言:

C语言中对字符和字符串的处理很频繁,但是C语言本身是没有字符串类型的,字符串通常放在常量字符串中或者字符数组中。
字符串常量适用于那些对它不做修改的字符串函数.
本文介绍的函数均在string.h头文件中


一、字符、字符串函数

1. strlen:求字符串长度

size_t strlen ( const char * str );

The length of a C string is determined by the terminating(结束) null-character: A C string is as long as the number of characters between the beginning of the string and the terminating null character (without including the terminating null character itself).

  • 字符串以'\0'作为结束标志,strlen函数返回的是在字符串中’\0’前面出现的字符个数(不包含’\0’)。
  • 参数指向的字符串必须要以’\0’结束。
  • 注意函数的返回值为size_t,是无符号

如:

#include <stdio.h> 
#include <string.h>
int main()
{
	const char* str1 = "Hello guys";
	const char* str2 = "Hello";
	if (strlen(str2) - strlen(str1) > 0)
	{
		printf("str2>str1\n");
	}
	else
	{
		printf("srt1>str2\n");
	}
	return 0;
}

输出结果为:str2>str1

无符号数恒大于0,if的判断条件恒为真。若想避免错误只需将if中的条件改为if (strlen(str2) > strlen(str1)即可。

strlen函数的模拟实现:

  • 计数器
  • 递归
  • 指针运算
#include <stdio.h>
#include <assert.h>
#include <string.h>
size_t my_strlen1(const char* str)
{
	assert(str);
	size_t cnt = 0;
	while (*str)
	{
		++cnt;
		++str;
	}
	return cnt;
}

size_t my_strlen2(const char* str)
{
	assert(str);
	return *str == '\0' ? 0 : my_strlen2(++str) + 1;
}

size_t my_strlen3(const char* str)
{
	assert(str);
	const char* start = str;
	const char* end = str;
	while (*end)
	{
		++end;
	}
	return end - start;
}

int main()
{
	char* s = "Hello World!";
	printf("%d %d\n", my_strlen1(s), strlen(s));
	printf("%d %d\n", my_strlen2(s), strlen(s));
	printf("%d %d\n", my_strlen3(s), strlen(s));
	return 0;
}

assert函数的作用是断言,若触发断言失败,则发出错误信号。在本例中,如果str为空指针,则使用ASSERT中止程序执行,调用该函数需包含assert.h头文件。

2. strcpy:字符串拷贝

char* strcpy (char * destination, const char * source );

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

  • 源字符串必须以’\0’结束。
  • 会将源字符串中的’\0’拷贝到目标空间。
  • 目标空间必须足够大,以确保能存放源字符串。
  • 目标空间必须可变。

strcpy函数的模拟实现

#include <stdio.h>
#include <assert.h>
#include <string.h>
char* my_strcpy(char* dest, const char* src)
{
	assert(dest && src);
	char* ret = dest;
	while (*dest++ = *src++)
	{
		;
	}
	return ret;
}
int main()
{
	char dest[30] = "Hello";
	char src[] = "guys";
	//my_strcpy(str1, str2);
	printf("%s\n", my_strcpy(dest,src));
    return 0;
}

3. strcmp:字符串比较

int strcmp ( const char * strl, const char * str2 );

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.

标准规定:

  • 第一个字符串大于第二个字符串,则返回大于0的数字
  • 第一个字符串等于第二个字符串,则返回0
  • 第一个字符串小于第二个字符串,则返回小于0的数字

strcmp函数的模拟实现:

#include <stdio.h>
#include <string.h>
#include <assert.h>
int my_strcmp(const char* str1,const char* str2)
{
	assert(str1 && str2);
	while (*str1 && *str1++ == *str2++);
	return *str1 - *str2;

}
int main()
{
	char str1[] = "abc";
	char str2[] = "abcd";

	int flag1 = strcmp(str1, str2);
	int flag2 = my_strcmp(str1, str2);

	if (flag1 > 0)
	{
		printf(">\n");
	}
	else if (flag1 < 0)
	{
		printf("<\n");
	}
	else 
	{
		printf("=\n");
	}
	if (flag2 > 0)
	{
		printf(">\n");
	}
	else if (flag2 < 0)
	{
		printf("<\n");
	}
	else 
	{
		printf("=\n");
	}

	return 0;
}

4. strcat:字符串追加

char * strcat ( char * destination, const char * source );

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.

  • 源字符串必须以’\0’结束。
  • 目标空间必须有足够的大,能容纳下源字符串的内容。
  • 目标空间必须可修改。

字符串能不能自己给自己追加?
不能,会陷入死循环。

strcat函数的模拟实现:

#include <stdio.h>
#include <assert.h>
#include <string.h>
char* my_strcat(char* dest, const char* src)
{
	assert(dest && src);
	char* ret = dest;
	while (*dest)
	{
		++dest;
	}
	while (*dest++ = *src++)
		;
	return ret;
}
int main()
{
	char str1[20] = "Hello";
	char str2[] = "guys";
	//printf("%s\n",strcat(str1, str2));
	printf("%s\n",my_strcat(str1, str2));
	return 0;
}

5. strncpy:指定长度字符串拷贝

char * strncpy ( char * destination, const char * source, size_t num );

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.

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

strncpy函数的模拟实现:

#include <stdio.h>
#include <string.h>
#include <assert.h>
char* my_strncpy(char* dest,const char* src,size_t count)
{
    assert(dest && src);
    char* start = dest;

    while (count && (*dest++ = *src++) != '\0')    /* 拷贝字符串 */
        --count;

    if (count)                              /* 补\0 */
    {
        while (--count)
            *dest++ = '\0';
    }

    return(start);
}

int main()
{
    //char str1[10] = "Hi";
    char str1[] = "Hello";
    char str2[] = "guys";
    my_strncpy(str1, str2, 3);
    //strncpy(str1, str2, 3);
    puts(str1);
    return 0;
}

在这里插入图片描述

6. strncmp:指定长度字符串比较

int strncmp ( const char * str1, const char * str2, size_t num );

  • 比较到出现另个字符不一样或者一个字符串结束或者num个字符全部比较完。
    在这里插入图片描述
    例:在所给字符串中找到形如"AAXX"的字符串
/* strncmp example */
#include <stdio.h> 
#include <string.h>
int main() 
{
	char str[][5] = { "AABB" , "ABBA" , "AABC" };
	int n;
	puts("Looking for AAXX strings...");
	for (n = 0; n < 3; n++)
	{
		if (strncmp(str[n], "AAXX", 2) == 0)
		{
			printf("found %s\n", str[n]);
		}
	}
	return 0;
}

在这里插入图片描述

strncmp函数的模拟实现:

int my_strncmp(const char* str1, const char* str2, size_t count)
{
    assert(str1 && str2);
    if (count == 0)
    {
        return 0;
    }
    while (*str1 && *str2 && *str1 == *str2 && count)
    {
        --count;
        ++str1;
        ++str2;
    }
    return *str1 - *str2;
}
int main()
{
    char str1[] = "a";
    char str2[] = "abcd";
    int ret1 = my_strncmp(str1, str2, 5);
    int ret2 = strncmp(str1, str2, 5);
    printf("%d %d\n", ret1,ret2);

    return 0;
}

7. strncat:指定长度字符串追加

char * strncat ( char * destination, const char * source, size_t num );

Appends the first num characters of source to destination, plus a terminating null-character.
If the length of the C string in source is less than num, only the content up to the terminating null-character is copied.

/* strncat example */ 
#include <stdio.h> 
#include <string.h>
int main() 
{
	char str1[20]; 
	char str2[20]; 
	strcpy(str1, "This is "); 
	strcpy(str2, "a string."); 
	strncat(str1, str2, 5); 
	puts(str1); 
	return 0;
}

在这里插入图片描述

strncat函数的模拟实现:

#include <stdio.h>
#include <string.h>
#include <assert.h>

char* my_strncat(char* dest, const char* src, size_t count)
{
    assert(dest && src);
    char* start = dest;

    while (*dest)
    {
        ++dest;
    }


    while (count--)
    {
        if ((*dest++ = *src++) == '\0')
        {
            return (start);
        }
    }

    *dest = '\0';
    return (start);
}

int main()
{
    char str1[20] = "Hello";
    char str2[] = "World";
    my_strncat(str1, str2, 3);
    //strncat(str1, str2, 3);
    puts(str1);
    return 0;
}

在这里插入图片描述

8. strstr:查找子串

char * strstr ( const char *str1, const char * str2);

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

例:

#include <stdio.h>
#include <string.h>
int main()
{
	char str[] = "www.csdn.net";
	char subStr[] = "csdn";
	char* ret = strstr(str, subStr);
	if (ret == NULL)
	{
		printf("%s子串不存在\n",subStr);
	}
	else
	{
		printf("%s子串存在\n",subStr);
	}
	return 0;
}

在这里插入图片描述

strstr函数的模拟实现:

leetcode题目链接:实现strstr()
查找子串有两种情况:
情况一:一次匹配找到子串
在这里插入图片描述

情况二:多次匹配找到子串

在这里插入图片描述

#include <stdio.h>
#include <string.h>
#include <assert.h>
char* my_strstr(const char* str1, const char* str2)
{
	assert(str1 && str2);
	const char* s1 = str1;
	const char* s2 = str2;
	const char* p = str1;

	while (*p)
	{
		s1 = p;//s1回到匹配开始的下一位置
		s2 = str2;//s2回到str2的起点
		while (*s1 != '\0' && *s2 != '\0' && *s1 == *s2)//向后匹配
		{
			s1++;
			s2++;
		}
		if (*s2 == '\0')//子串走完,匹配成功
		{
			return (char*)p;
		}
		p++;
	}
	return NULL;
}
int main()
{
	char str[] = "www.csdn.net";
	char subStr[] = "csdn";
	char* ret = my_strstr(str, subStr);
	if (ret == NULL)
	{
		printf("%s子串不存在\n",subStr);
	}
	else
	{
		printf("%s子串存在\n",subStr);
	}
	return 0;
}

9. strtok:字符串切割

char *strtok( char *strToken, const char *strDelimit );

  • strDelimit 参数是个字符串,定义了用作分隔符的字符集合
    第一个参数指定一个字符串,它包含了0个或者多个由strDelimit 字符串中一个或者多个分隔符分割的标记。
  • strtok函数找到str中的下一个标记,并将其用\0结尾,返回一个指向这个标记的指针。(注: strtok函数会改变被操作的字符串,所以在使用strtok函数切分的字符串一般都是临时拷贝的内容 并且可修改。)
  • strtok函数的第一个参数不为NULL,函数将找到str中第一个标记,strtok函数将保存它在字符串中的位置。
  • strtok函数的第一个参数为NULL,函数将在同一个字符串中被保存的位置开始,查找下一个标 记。
  • 如果字符串中不存在更多的标记,则返回NULL指针。
#include <stdio.h>
#include <string.h>
int main()
{
	char str[] = "https://www.csdn.net/";
	const char* strDelimit = ":/.";
	char cp[30] = { 0 };
	strcpy(cp, str);

	char* ret = NULL;
	for (ret = strtok(cp, strDelimit); 
		 ret != NULL;
		 ret = strtok(NULL,strDelimit))
	{
		puts(ret);
	}
	return 0;
}

在这里插入图片描述

10. strerror:错误报告

char * strerror ( int errnum );

C语言的库函数,在执行失败时都会设置错误码
errno 是C语言设置的一个全局的错误码存放的变量
strerror函数返回错误码所对应的错误信息。

例:

#include <stdio.h>
#include <string.h>
#include <errno.h>//必须包含的头文件
int main()
{
	FILE* pFile;
	pFile = fopen("test.txt", "r");
	if (pFile == NULL)
	{
		printf("%s\n", strerror(errno));
	}

	return 0;
}

输出:
在这里插入图片描述

11. 字符分类函数:

函数如果他的参数符合下列条件就返回真
iscntrl任何控制字符
isspace空白字符:空格‘ ’,换页‘\f’,换行’\n’,回车‘\r’,制表符’\t’或者垂直制表符’\v’
isdigit十进制数字 0~9
isxdigit十六进制数字,包括所有十进制数字,小写字母a~f,大写字母A~F
islower小写字母a~z
isupper大写字母A~Z
isalpha字母a~z或A~Z
isalnum字母或者数字,a~z,A~Z,0~9
ispunct标点符号,任何不属于数字或者字母的图形字符(可打印)
isgraph任何图形字符
isprint任何可打印字符,包括图形字符和空白字符

12. 字符转换函数:

int tolower ( int c );
int toupper ( int c );

例:

#include <stdio.h>
#include <ctype.h>
int main()
{
	int i = 0;
	char str[] = "This is a String.\n";
	char c;
	while (str[i])
	{
		c = str[i];
		if (islower(c))
		{
			c = toupper(c);
		}
		putchar(c);
		i++;
	}
	return 0;
}

在这里插入图片描述

二、内存操作函数

1. memcpy:·

void * memcpy ( void * destination, const void * source, size_t num );

  • 函数memcpy从source的位置开始向后复制num个字节的数据到destination的内存位置。
  • 这个函数在遇到’\0’的时候并不会停下来。
  • 如果source和destination有任何的重叠,复制的结果都是未定义的。

memcpy函数的模拟实现

void*为泛型指针可以接收任意类型指针,但不能解引用和指针运算,所以操作void*指针时需要强制类型转换

#include <stdio.h>
#include <assert.h>
#include <string.h>
void* my_memcpy(void* dest, const void* src, size_t num)
{
	assert(dest && src);
	void* ret = dest;
	while (num--)
	{
		*((char*)dest) = *((char*)src);
		dest = (char*)dest + 1;
		src = (char*)src + 1;
	}
	return ret;
}
int main()
{
	int arr1[] = { 0,2,4,6,8 };
	int arr2[] = { 1,3,5,7,9 ,11,13 };
	my_memcpy(arr1, arr2, 20);
	for (int i = 0; i < 5; ++i)
	{
		printf("%d ", arr1[i]);
	}
	return 0;
}

如果source和destination有重叠,复制的结果是什么呢
例如:

#include <stdio.h>
#include <assert.h>
#include <string.h>
void* my_memcpy(void* dest, const void* src, size_t num)
{
	assert(dest && src);
	void* ret = dest;
	while (num--)
	{
		*((char*)dest) = *((char*)src);
		dest = (char*)dest + 1;
		src = (char*)src + 1;
	}
	return ret;
}
int main()
{
	int arr[] = { 1,2,3,4,5,6,7,8,9 };
	my_memcpy(arr+2, arr, 20);
	for (int i = 0; i < 10; ++i)
	{
		printf("%d ", arr[i]);
	}
	return 0;
}

上面代码的本意是将1 2 3 4 5拷贝到3 4 5 6 7的位置去
预期输出为:1 2 1 2 3 4 5 8 9
但事与愿违,运行后发现输出结果为:
在这里插入图片描述
为什么呢?
因为在拷贝过程中后面的数据会被前面的数据覆盖。

但在VS环境下直接使用库函数memcpy发现可以达到预期效果,是不是我们自己的实现有问题呢,然而并不是。这种情况是标准库未定义的,我们实现的代码是刚好可以满足memcpy函数的需求,刚好及格;而VS的该函数的实现是满分代码。标准库中有memmove函数能满足这种需求。

2. memmove:

void * memmove ( void * destination, const void * source, size_t num );

  • 和memcpy的差别就是memmove函数处理的源内存块和目标内存块是可以重叠的。
  • 如果源空间和目标空间出现重叠,就得使用memmove函数处理。

memmove函数的模拟实现:

通过dest和src指针的位置(大小)关系可以区分出是从前往后处理数据还是从后往前处理数据。

在这里插入图片描述

#include <stdio.h>
#include <assert.h>
#include <string.h>
void* my_memmove(void* dest, const void* src, size_t num)
{
	assert(dest && src);
	void* ret = dest;
	if (dest < src)
	{
		//前->后
		while (num--)
		{
			*((char*)dest) = *((char*)src);
			dest = (char*)dest + 1;
			src = (char*)src + 1;
		}
	}
	else
	{
		while (num--)
		{
			//后->前
			*((char*)dest + num) = *((char*)src + num);

		}
	}
	return ret;
}
int main()
{
	int arr[] = { 1,2,3,4,5,6,7,8,9 };
	my_memmove(arr+2, arr, 20);
	//memmove(arr+2, arr, 20);
	for (int i = 0; i < 9; ++i)
	{
		printf("%d ", arr[i]);
	}
	return 0;
}

3. memcmp:

int memcmp
( const void * ptr1,
const void * ptr2,
size_t num );

Compare characters in two buffers.
比较两个缓冲区中的字符。

  • 比较从ptr1和ptr2指针开始的num个字节
  • 返回值如下:
    在这里插入图片描述
#include <string.h>
#include <stdio.h>

void main(void)
{
    char first[] = "12345678901234567890";
    char second[] = "12345678901234567891";
    int result;

    printf("Compare '%.19s' to '%.19s':\n", first, second);
    result = memcmp(first, second, 19);
    if (result < 0)
        printf("First is less than second.\n");
    else if (result == 0)
        printf("First is equal to second.\n");
    else if (result > 0)
        printf("First is greater than second.\n");
    printf("Compare '%.20s' to '%.20s':\n", first, second);
    result = memcmp(first, second, 20);
    if (result < 0)
        printf("First is less than second.\n");
    else if (result == 0)
        printf("First is equal to second.\n");
    else if (result > 0)
        printf("First is greater than second.\n");
}

在这里插入图片描述

4. memset:

void *memset( void *dest, int c, size_t count );

Sets buffers to a specified character.
将缓冲区设置为指定字符。

#include <string.h>
#include <stdio.h>

void main(void)
{
	char buffer[] = "This is a test of the memset function";
	
	printf("Before: %s\n", buffer);
	memset(buffer, '*', 4);
	printf("After:  %s\n", buffer);
}

在这里插入图片描述


总结

以上就是本文所讲的内容,如有错误,请指正。🌹

  • 3
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 2
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

二木 同学

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值